Search code examples
javaoffice365jakarta-mail

Getting Exception while using Java Mail for Office 365


I am getting below exception while sending mails using Java Mail for Office365 Exchange server through our application in Windows Server. The Windows server is located in cloud platform. The same configuration(Exchange server details) with our application is working in windows 11 personal laptop and another Windows Servers ( is located in different cloud vendor) .

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Unknown SMTP host: smtp.office365.com;
  nested exception is:
    java.net.UnknownHostException: smtp.office365.com. Failed messages: javax.mail.MessagingException: Unknown SMTP host: smtp.office365.com;
  nested exception is:
    java.net.UnknownHostException: smtp.office365.com; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Unknown SMTP host: smtp.office365.com;
  nested exception is:
    java.net.UnknownHostException: smtp.office365.com
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1959)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
    at com.grc.jframework.mail.FailsafeMailSender.sendInternal(FailsafeMailSender.java:146)
    at com.grc.jframework.mail.FailsafeMailSender.lambda$init$0(FailsafeMailSender.java:115)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.UnknownHostException: smtp.office365.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 11 more

Is there anything to change/update configuration at Windows Server side?

Below are the SMTP Server details:

smtp.sender.main.port = 587

smtp.sender.reserve.auth_type = ANONYMOUS

smtp.sender.main.auth_type = TLS

smtp.sender.main.host = smtp.office365.com

smtp.sender.reserve.port = 25

smtp.sender.main.timeout_sec = 90


Solution

  • As @Mark Rotteveel suggested in comments, it was the issue with smtp.office365.com DNS and resolved with admin team.