我使用spring boot2。我尝试发送邮件。服务器是我的build.gradle中的Outlook
compile('org.springframework.boot:spring-boot-starter-mail')
在我的门面课中
for (FactoryEmailNC factoryEmail : factoryEmails) {
String message = mailContentBuilder.build(factoryEmail);
if (factoryEmail.getEmails() != null && !factoryEmail.getEmails().isEmpty()) {
mailService.sendHtmlMail(factoryEmail.getEmails(), "Not compliant", message);
//query to specify email has been sent.
setSampleEmailSent(factoryEmail);
}
}
private void setSampleEmailSent(FactoryEmailNC factoryEmail) {
....
samplesServices.setEmailsSent(testSampleIdEmailSent);
}
在我的SamplesServices类中
@Transactional
public void setEmailsSent(Map<String, List<SampleId>> testSampleIdEmailSent){
//call to repository, set flag email sent to true
...
}
public class MailServiceImpl(){
@Autowired
private JavaMailSender javaMailSender;
@Async
public void sendHtmlMail(List<String> to, String subject, String body) throws MessagingException {
MimeMessage mail = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
...
helper.setTo(to.stream().toArray(String[]::new)); //line 64
javaMailSender.send(mail);
}
}
实际上已发送电子邮件,但似乎未调用setSampleEmailSent,因为电子邮件标志仍为false
org.springframework.mail.MailSendException:失败消息: javax.mail.MessagingException:异常读取响应;嵌套的 异常是:java.net.SocketTimeoutException:读取超时于 org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:490) 〜[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]在 org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:360) 〜[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]在 org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:355) 〜[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]在 com.mermacon.service.MailServiceImpl.sendHtmlMail(MailServiceImpl.java:64) 〜[classes!/:na]
在我的应用程序属性中
spring.mail.properties.mail.smtp.connectiontimeout = 5000
spring.mail.properties.mail.smtp.timeout = 3000
spring.mail.properties.mail.smtp.writetimeout = 5000
spring.mail.properties.from=info@meracon.com
spring.mail.host = mail.oldubi.com
spring.mail.port = 25
知道为什么发送电子邮件但仍然超时吗?
编辑1
仅当我发送多封电子邮件时我才会超时 发送1封电子邮件的日志文件 https://pastebin.com/6y6n8MV5
发送1封电子邮件后的日志文件 https://pastebin.com/j2sT7qHu
编辑2
我在发送的每封电子邮件之间设置了一个线程睡眠,超时消失