How should a Java program work with an external mail server?

I have a constantly running Java program that should send email whenever it encounters a problem. However, it is possible that the mail server that it uses may not be available while it is trying to send an email.

What is the best way to ensure email delivery when returning a mail server?

+4
source share
7 answers

Request Queues Have a separate thread that just waits for something to enter the queue, then tries to send it by email. If he fails, he waits several hours and tries again. After sending a message, he returns to the queue to receive the next message.

+9
source

Put the email object on the stack or list when it cannot send, when the email server returns, pull out each email address until it becomes empty.

+2
source

You might want to save the email in a file, perhaps in an xml file, so that if the application crashes, you will not lose this information.

This file is downloaded when the application starts and it stores everything in memory, so when there are pending emails, it continues to be checked every 5 minutes or so, and then, as it sends each email, it will save the xml file so that if it works after sending 3 out of 10 emails, it will not resend these three when it starts.

But how you handle this will actually determine the specification of how to handle error conditions.

+2
source

If you go from โ€œredirect everything to this SMTP server, which is always thereโ€, to a situation where you need to handle all types of conditions that are usually handled by a full SMTP server, for example, try again, retransmit if the connection is closed, use MX -hosts in their declared order and the like, you might just want to have an SMTP server inside your client (but one that does not accept incoming connections), as this pushes all the dirty logic away from your applications.

I find that the James mail server - http://james.apache.org/ - is easy to embed, but I haven't really tried.

+2
source

The suggestion to use James is a good one, but I had some problems in the past when James was a little flaky and needed a restart.

You can use something like Quartz to check the scheduler of the messages you need to send. If the message cannot be sent (for example, the smtp server is unavailable), this message is postponed to a later date. You may have a task for each message, or have an ongoing task that checks for messages and an available mail server, and then sends messages. An ongoing task will give you an email batch.

0
source

If you are in the Unix / Linux world, consider an alternative to sending your alerts using syslog and work with email generation from this side. For example, nsyslogd has a module called ommail for creating emails natively.

IIRC, there are adapters for log4j and the like, which can connect between the Java and syslog worlds with minimal (zero?) Coding.

0
source

Apache James - http://james.apache.org/ will allow you to run your own mail server as a proxy server, not only, but written in 100% Java, so you can understand what it does, and as an additional Bonus James uses databases to queue mail, so you can even enter mail directly into the queues by inserting them into the database, and then leave all the work of sending mail to James.

0
source

All Articles