简体   繁体   中英

Java Mail Not connected exception, but email sent out

I am experiencing an issue where JAVA mail (JavaMail version 1.5.3) throws a Not Connected exception when sending email messages and the email does get sent out. I have been encountering this since 4 weeks ago. The issue seems mostly related to hosted exchange servers. The exception is as follows:

java.lang.IllegalStateException: Not connected com.sun.mail.smtp.SMTPTransport.checkConnected(SMTPTransport.java:2355) com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1151) com.email.sender.EmailSenderThread.sendEmailMessages(EmailSenderThread.java:127) com.email.sender.EmailSenderThread.threadProcess(EmailSenderThread.java:59) com.email.util.PhaseThread.run(PhaseThread.java:40) java.util.concurrent.Executors$RunnableAdapter.call(Executors.Z93F725A07423FE1C889F448B33D2 1F46Z:511) java.util.concurrent.FutureTask.run(FutureTask.java:266) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) java.lang.Thread.run(Thread.java:748)

I am working on getting the debug logs for the exception, but I was just wondering how this can be prevented as well as if there was an update rolled out to Exchange, because the code was working perfectly for years.

Thanks in advance !!!

If you're "pooling" the Transport object so it can be reused, the server is probably dropping the connection due to inactivity.

As for why the message is still sent even when you get this exception, your application must be doing something to reconnect after getting the exception.

I did get the same exception since I forgot to check this. After adding a check everything worked again:

if (!mailSession.getTransport().isConnected()) {
 //...reconnection code
 Transport newTransport = mailSession.getMailSession().getTransport();
 newTransport.connect(...);
 mailSession.setTransport(newTransport);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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