Java Spring Boot电子邮件发送抛出SocketTimeoutException:读取超时

Java Spring Boot电子邮件发送抛出SocketTimeoutException:读取超时,java,spring,email,spring-boot,smtp,Java,Spring,Email,Spring Boot,Smtp,我已经研究这个话题好几天了,但是我在网上找到的答案没有一个对我有用 上下文:我有一个SpringBootWeb应用程序,它使用Java邮件API和SpringBootStarter邮件发送自动电子邮件通知 它正在使用GMail SMTP服务器和GSuite帐户。我最近升级到使用Spring5.0.6和SpringBoot2.0.2,电子邮件发送停止工作 一些线索: 发送电子邮件的Java代码与以前相同 Gmail SMTP仍然可以从另一个VM正常工作,使用具有相同设置和身份验证的旧版本应用程序,

我已经研究这个话题好几天了,但是我在网上找到的答案没有一个对我有用

上下文:我有一个SpringBootWeb应用程序,它使用Java邮件API和SpringBootStarter邮件发送自动电子邮件通知

它正在使用GMail SMTP服务器和GSuite帐户。我最近升级到使用Spring5.0.6和SpringBoot2.0.2,电子邮件发送停止工作

一些线索:

发送电子邮件的Java代码与以前相同 Gmail SMTP仍然可以从另一个VM正常工作,使用具有相同设置和身份验证的旧版本应用程序,电子邮件发送正确。 除非我遗漏了什么,否则应用程序配置与以前一样 发生了变化的事情:

升级到Spring 5.0.6 升级至Spring Boot 2.0.2 在Java代码中的许多地方进行了更改,以匹配此升级,并在应用程序的其他部分添加了功能 VM的IP地址与AWS EC2实例之前不同 以下是pom.xml中的相关依赖项:

    <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
这些变量在环境中定义:

FT_MAIL_SMTP_HOST=smtp.gmail.com
FT_MAIL_SMTP_PORT=587
FT_MAIL_SMTP_USERNAME=myaccount@myapp.com
FT_MAIL_SMTP_PASSWORD=mypassword

FT_MAIL_SMTP_STARTTLS=true
FT_MAIL_SMTP_TLSREQUIRED=true
FT_MAIL_SMTP_AUTH=true
FT_MAIL_SMTP_CONN_TIMEOUT=5000
FT_MAIL_SMTP_TIMEOUT=5000
FT_MAIL_SMTP_WRITE_TIMEOUT=5000
以下是用于发送电子邮件的Spring@服务:

@Service
public class EmailServiceImpl {

@Autowired
public JavaMailSender emailSender;

@Autowired
private SpringTemplateEngine templateEngine;

@Value("${myapp.mail.from}")
private String fromAddress;

@Value("${myapp.mail.replyto}")
private String replyToAddress;

public void sendTemplatedMessage(String template, String to, String subject, Map<String, Object> model) throws MailException, MessagingException {  
    sendTemplatedMessage(template, to, fromAddress, replyToAddress, subject, model);
}

public void sendTemplatedMessage(String template, String to, String from, String subject, Map<String, Object> model) throws MailException, MessagingException { 
    sendTemplatedMessage(template, to, from, replyToAddress, subject, model);
}

private void sendTemplatedMessage(String template, String to, String from, String replyTo, String subject, Map<String, Object> model) throws MailException, MessagingException {    

    MimeMessage message = emailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(message,
            MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
            StandardCharsets.UTF_8.name());

    //helper.addAttachment("logo.png", new ClassPathResource("memorynotfound-logo.png"));
    Context context = new Context();
    context.setVariables(model);
    String html = templateEngine.process(template, context);

    helper.setTo(to);
    helper.setFrom(from);
    helper.setReplyTo(from);
    helper.setSubject(subject);
    helper.setText(html, true);

    emailSender.send(message);
}

public void sendSimpleMessage(String to, String from, String subject, String text) {
    try {           
        SimpleMailMessage message = new SimpleMailMessage(); 
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject); 
        message.setText(text);
        emailSender.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
我尝试设置错误的SMTP服务器或错误的凭据,这导致连接失败,因此我假设服务器和凭据是正确的,并且错误发生在成功连接之后

使用的帐户尚未达到其限制,因为另一个VM使用相同的凭据并发送电子邮件没有问题

我已尝试将Start TLS设置更改为false,并改用端口465,但这也不起作用


感谢您的帮助!!提前谢谢

在对配置进行了更多的尝试和错误之后,我发现配置中需要应用程序属性spring.mail.protocol

我在application.yml中添加了行协议:smtp:

spring:
  mail:
    protocol: smtp

这解决了读取超时问题,电子邮件现在可以正常发送。希望这可能会对将来的人有所帮助。

在对配置进行了更多的尝试和错误之后,我发现配置中需要一个应用程序属性spring.mail.protocol

我在application.yml中添加了行协议:smtp:

spring:
  mail:
    protocol: smtp

这解决了读取超时问题,电子邮件现在可以正常发送。希望这能对将来的人有所帮助。

我确实面临同样的问题,但我的情况有点不同

我试着用quartz按时间表发送

当我不使用石英时,一切正常,但使用石英时,它开始失效

上述解决方案对我没有帮助,但为我指明了查看已设置属性的方向

增加连接超时为我完成了这项工作

因此更改了应用程序属性

发件人:

致:


希望它也适用于其他人

我确实面临同样的问题,但我的情况有点不同

我试着用quartz按时间表发送

当我不使用石英时,一切正常,但使用石英时,它开始失效

上述解决方案对我没有帮助,但为我指明了查看已设置属性的方向

增加连接超时为我完成了这项工作

因此更改了应用程序属性

发件人:

致:

希望它对其他人也有用

spring:
  mail:
    protocol: smtp
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.timeout=25000