Help connecting SMTP server when sending emails from Java

I use an external mail server to send SMTP letters, this server is uncontrollable.

Several times recently there have been problems with this mail server, and this has led to the Java application (Struts / Spring) completely freezing while waiting for a response from the mail server.

I am using Spring org.springframework.mail.javamail.JavaMailSender to send emails.

When problems arise on the external mail server, this is the next line that freezes mailEngine.send (mailMessage);

I don’t mind that sometimes emails are not sent, but how can I stop this from freezing my application while it waits for a response from the SMTP server?

Are there any good email queue solutions for Java?

+3
source share
2 answers

You can send letters in the background thread.

+1
source

Call your calls to the SMTP server. You can use ExecutorService (there are various implementations) and drop it Runnablesto a later stage (from the group). The advantage of this approach is that you do not need to explicitly specify your thread model.

If you send a Future object from the Contractor after sending, you can call get()with a suitable timeout and cancel (and possibly resend / retry) with a timeout.

+1

Source: https://habr.com/ru/post/1735046/


All Articles