简体   繁体   English

如何在代理后面的 Spring Boot 中使用 JavaEmailSender

[英]How to use JavaEmailSender in Spring Boot behind a proxy

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我已经使用 JavaEmailSender 在 Spring Boot 中设置了电子邮件发送,我正在尝试在代理后面使用它我已经尝试将以下代理配置添加到 application.properties 文件,但我收到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: Java电子邮件发送器:

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

    }

This looks like connectivity issue.You can use ping and also check by using telnet or putty.这看起来像连接问题。您可以使用 ping 并使用 telnet 或 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下面是 telnet 连接在我机器上的腻子中的样子,这基本上意味着 smtp.gmail.com 可以访问端口 587

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

在此处输入图片说明

在此处输入图片说明

Try setting the Proxy on your main method through the following lines:尝试通过以下几行在您的main方法上设置代理:

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.我遇到了同样的问题,这为每个顽固的图书馆设置了代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM