简体   繁体   中英

How should a Java program handle an external mail server being down?

I have a constantly-running Java program that needs to send an email whenever it encounters a problem. However it is possible that the mail server it uses could be down at the time it tries to send the email.

What is the best way to ensure that the email will be delivered when the mail server comes back up?

Queue up the requests. Have a separate thread which merely waits for something to enter the queue, then tries to email it. If it fails, it waits a few hours and tries again. Once it sends a message, it goes back to the queue to get the next message.

当电子邮件服务器无法发送时,将电子邮件对象放入堆栈或列表中,当电子邮件服务器恢复时,将每个电子邮件弹出,直到它为空。

You may want to save the email in a file, perhaps an xml file, so that should the application crash you won't lose this information.

This file is loaded when the application starts, and it keeps everything in memory, so that while there are pending emails then it keeps checking every 5 minutes or so, then, as it sends each email it will resave the xml file, so that should it crash after sending 3 emails out of 10 it won't resend those three when it starts up.

But, how you handle that is really going to depend on the specification for how to handle error conditions.

If you go from "forward everything to this SMTP server which is always there" to a situation where you need to handle all kinds of conditions normally handled by a full SMTP-server like retry later, retransmit if connection closed, use MX-hosts in their stated order and similar, you may want to consider simply having a SMTP-server inside your client (but one that does not accept incoming connections) since this moves all the dirty logic away from your applications.

I believe that the James email server - http://james.apache.org/ - is easily embeddable, but I have not actually tried.

The suggestion of using James is a good one but I've had some issues in the past of James being a bit flaky and needing to be restarted.

You could use something like Quartz to have a scheduler check for messages that need to be sent. If the message can't be sent (eg. smtp server isn't available), then that message is rescheduled to be sent at a later time. You could either have a task per message or have a persistent task that checks for messages and available mail server then sends the messages. The persistent task would give you email batching.

If you are in a Unix/Linux world, then consider the alternative of sending your alerts using syslog , and dealing with the generation of emails on that side. For example, nsyslogd has a module called ommail for generating emails natively.

IIRC, there are adapters for log4j and the like that can bridge between the Java and syslog worlds with a minimum of (zero ?) coding.

Apache James - http://james.apache.org/将允许您运行自己的邮件服务器作为代理,不仅如此,而且是用100%java编写的,因此您可以弄清楚它的作用,并作为额外的奖励James使用数据库对邮件进行排队,因此您甚至可以通过插入数据库将邮件直接注入队列,然后将整个邮件发送给James。

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