0

javax.mail.MessagingException: Connection reset; nested exception is: java.net.SocketException: Connection reset

I have a service to read mails from outlook. Receiving the above error randomly. What would be the root cause of this issue. Using JAVA 8 and javax.mail-1.6.2 to read mails.

Below is my mail settings

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "outlook.office365.com"); 
props.put("mail.smtp.socketFactory.port", "587"); 
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
3
  • How often does the error occur? Commented May 12, 2021 at 12:34
  • there could be a (conection) leak in your code (like here) ..or if you operate your jakarta "within a cloud" read here (kubernetes) Commented May 12, 2021 at 12:38
  • @MarkTobin this error occurs randomly once or twice in a day Commented May 12, 2021 at 14:42

1 Answer 1

0

The problem is more likely to be a network issue than an issue with your application. It might be worth having a look here to see if any of those probable causes are likely or possible in your circumstance. Having had a similar issue with outlook previously with no root cause established, mitigation is also an option. Below shows a basic retry mechanism which, in practical terms, eliminated the issue.

            // send email with try-catch loop 
            int count = 0;
            int maxTries = 5;
            while(true) {
                try {
                    // get and set your protocols
                    mailSender.send(message);
                    break;
                } catch (Exception e) {
                    if (++count == maxTries) {
                        // Give up and throw the error
                        throw e;
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.