Reputation: 1474
I am trying to develop a email client. which sends email to the given recipients
using System.Net;
using System.Net.Mail;
MailMessage msg;
SmtpClient client;
SMTPURL=abc.xyz
SMTPPort=87
client = new SmtpClient(SMTPURL, SMTPPort);
client.Credentials = new NetworkCredential(senderID, senderPWD);
msg = new MailMessage();
msg.To.Add("[email protected]");
msg.Body="hello hi bye";
client.Send(msg);
this code is working well, but I have a backup email server with URL 123.xyz
if my abc.xyz is down or I have wrong url I will get a SMTPException
Now my question is how to reroute my message to 123.xyz backup mail server
My assumption is to catch the SMTPException and change the SMTPURL to 123.xyz and resend, but is this a good way or any other alternates exists to reroute to secondary mail server ?
Thanks in advance
Upvotes: 0
Views: 649
Reputation: 1149
you should be able to use your basic try/catch block:
public void function sendemail()
{
try{
SendEmailByServer(primaryserverurl);
}
catch(SMTPException se)
{
sendemailbyserver(backupurl);
}
catch(Exception ex)
{
//something else broke
}
}
public void function SendEmailByServer(string server)
{
MailMessage msg;
SmtpClient client;
SMTPURL=server;
SMTPPort=87;
client = new SmtpClient(SMTPURL, SMTPPort);
client.Credentials = new NetworkCredential(senderID, senderPWD);
msg = new MailMessage();
msg.To.Add("[email protected]");
msg.Body="hello hi bye";
client.Send(msg);
}
Upvotes: 1