Home > Software engineering >  How to use JavaEmailSender in Spring Boot behind a proxy
How to use JavaEmailSender in Spring Boot behind a proxy

Time:10-12

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);

    }

CodePudding user response:

This looks like connectivity issue.You can use ping and also check by using telnet or putty. Below is how a telnet connection looks like in putty from my machine which basically means the port 587 is accessible for smtp.gmail.com

ping smtp.gmail.com

Pinging smtp.gmail.com [74.125.24.108] with 32 bytes of data:
Reply from 74.125.24.108: bytes=32 time=75ms TTL=109
Reply from 74.125.24.108: bytes=32 time=74ms TTL=109
Reply from 74.125.24.108: bytes=32 time=75ms TTL=109
Reply from 74.125.24.108: bytes=32 time=74ms TTL=109

enter image description here

enter image description here

CodePudding user response:

Try setting the Proxy on your main method through the following lines:

System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "8080");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1|10.*.*.*");
System.setProperty("https.proxyHost", "host");
System.setProperty("https.proxyPort", "8080");
System.setProperty("https.nonProxyHosts", "localhost|127.0.0.1|10.*.*.*");
System.setProperty("java.net.useSystemProxies", "true");

I have had the same problem and this set the proxy for every library which was stubborn.

  • Related