Sending Email From DigitalOcean

On my development machine, the code works perfectly fine as it sends out emails smoothly with reports. However, the same code fails terribly when I run it on digitalocean.

Error message is related to authentication. Logs look like the following:

2020-07-19 12:51:13,064 ERROR o.a.j.l.DirectJDKLog [http-nio-11111-exec-2] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: ;
  nested exception is:
        javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out] with root cause
java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method) ~[?:1.8.0_151]

digitalocean_hotmail

Search results from internet show that lots of guys are having this issue, while some blame on spring mail, and others blame on gmail settings. I got the hint from the settings on gmail for less secured mode. While hotmail doesn’t have such a setting, it’s most likely related to the mail server security mechanism than the standard library.

From my machine, hotmail should have already recognized my IP when I connect via browser; so the program would be recognized automatically. However, hotmail couldn’t recognize my digitalocean IP, or it could be the problem of digitalocean host by itself. So in any case, the guideline to use digitalocean to access hotmail without using java.

I tried the following steps, and the failing problem is now starting sending emails, just as tested on my dev machine. So it proved my hypothesis on the issue.
1. from digitalocean ssh session, telnet to ports on smtp host; to check that digitalocean is actually connecting. If the firewall is blocking, then you should know where to fix. For my case, it connects fine.
2. login to hotmail account, and check “Review recent activity”.
recent_activity
3. while reviewing, just tell hotmail that those IPs are actually from yourself.
4. after acknowledgement from hotmail, try to run the same service again after 30 minutes.
5. it should start sending emails, like from your machine.

I didn’t do any code changes, and the same set of codes are working now.

Reference:
1. https://www.digitalocean.com/community/questions/gmail-smtp-not-able-to-send-email-javax-mail-authenticationfailedexception
2. https://stackoverflow.com/questions/59883024/org-springframework-mail-mailauthenticationexception-authentication-failed-nes

Published by

wofong

三千娑婆世界,三千难忘遗憾;回头乃是岸,此岸在何方;堪忍不能忍,万般看不穿;何时放得下,始得自在心。 I'm a programmer, a quantitative analyst, a photography hobbyist, a traveler, a runner, and a nature lover.

One thought on “Sending Email From DigitalOcean”

Leave a comment