views:

16

answers:

2

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="[email protected]">
        <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?

+1  A: 

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.

Darin Dimitrov
+1  A: 
<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).

just somebody
I'll accept this as an answer because I agree that the redundancy is better managed by an SMTP relay server, rather than in C# business logic.
JL