0

I use spring boot 2. I try to send mail. Server is outlook In my build.gradle

compile('org.springframework.boot:spring-boot-starter-mail')

In my facade class

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

In my SamplesServices class

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

Actually email is sent, but setSampleEmailSent don't seem to be called because email flag still to false

org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketTimeoutException: Read timed out at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:490) ~[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:360) ~[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE] at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:355) ~[spring-context-support-5.1.5.RELEASE.jar!/:5.1.5.RELEASE] at com.mermacon.service.MailServiceImpl.sendHtmlMail(MailServiceImpl.java:64) ~[classes!/:na]

In my application properties

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

Any idea why email is sent but still get timeout?

Edit 1

I get timeout only if i send more then one email Log file of sending 1 email https://pastebin.com/6y6n8MV5

Log file after sending 1 email https://pastebin.com/j2sT7qHu

Edit 2

I put a thread sleep between every email sent, timeout disapear

Community
  • 1
  • 1
robert trudel
  • 5,283
  • 17
  • 72
  • 124
  • Looks like there is failure in handshake and thus you are not receiving acknowledgement back. Did you try using `587` port ? 25 was more plaintext conventional port. I am assuming your outlook server expects TLS connection, thus 587 is recommended. Outlook config https://stackoverflow.com/questions/44405168/how-to-send-mail-via-outlook-using-spring-boot – Amith Kumar Mar 21 '19 at 18:47
  • don't get timeout if i send only one email, so port is ok – robert trudel Mar 22 '19 at 00:41

0 Answers0