I've setup email sending in Spring Boot using JavaEmailSender, i'm trying to use it behind a proxy i've tried to add below proxy configuration to application.properties file but i'm getting Connection timed out error
Error :
java.net.ConnectException: Connection timed out: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect; message exceptions (1) are:
Failed message 1: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
Email Configuration :
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=myemail@gmail.com
spring.mail.password=qypaaboydxppgpck
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Proxy Configuration :
mail.smtp.proxy.host=http://proxy
mail.smtp.proxy.port=8080
mail.smtp.proxy.user=username
mail.smtp.proxy.password=*******
JavaEmailSender:
@Autowired
private JavaMailSender emailSender;
@Autowired
SpringTemplateEngine templateEngine;
public void sendSimpleMessage(String to, String subject, List<SiteMailDTO> listSiteMailDTO) throws MessagingException {
Properties props = emailSender.;
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());
Map<String, Object> model = new HashMap<String, Object>();
model.put("sites", listSiteMailDTO);
Context context = new Context();
context.setVariables(model);
String html = templateEngine.process("email-template", context);
try {
helper.setTo(to);
helper.setText(html, true);
helper.setSubject(subject);
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
}
emailSender.send(message);
}

