asp.net - how to make use of 2 or more email servers for failsafe
I have 开发者_开发知识库code that relies heavily on email notification. In my web.config I am able to specify an smtp server like this:
<system.net>
<mailSettings>
<smtp from="myaccount@mydomain.com">
<network host="mail.mydomain.com" port="25" userName="myusername" password="mypassword" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
This is acceptable, but I would like to implement 2 or 3 exchange servers here in the event that (for what ever reason) smtp server 1 goes offline, I need a backup option.
Is there any quick / built in way to achieve this fail safe in .net, or is there a trusted manual way to implement this. My existing send mesage code looks like this (but watered down):
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
client.Send(message);
Notice its pulling the host directly from the configuration.
Any ideas what is best practice here for this scenario?
<network host="localhost" port=...
and configure the local SMTP transport to relay to your rendundant servers. that way you won't loose emails because of transient network problems (the local MTA will just hold on to them).
There's an SmtpClient constructor that takes a host name and port. You could have a list of servers, try send the mail and if the server is unavailable it will throw an exception which you catch and retry with other server from the list.
精彩评论