Logging/Backup mails on mail server failure or mail sending failure

Hi folks,

In this post, i am talk about the mail back up mechanism when the mail server down or mail sending failed due to some mail server error.

Let’s consider the scenario of applying for a job online. Job seekers will fill the online form and apply for the jobs by uploading their resumes. what will happen if the mail server has some problem at that time, and there is now way to track the application unless the application call and inform.

you want get the job seeker application by mail

the job seeker will think his application has been passed to you.

To avoid this situation, this mechanism propose to backup the job application mails if the mail server under some problem.

First we need to catch the events that occur when the SmtpClient finishes asynchronously sending an e-mail message using the SendAsync method.

Check if there any server error on mail send (when SendCompletedEvent event fired) and call the Save method to backup the mail.

 

Finally Save the mail

In order to save the mail, we have to convert the System.Net.Mail.MailMessage object to message file format, so we can open it later on Outlook and see the content. But it’s a complex work, so I’m using a trick to achieve this.

You guys know that if we defined SmtpDeliveryMethod.SpecifiedPickupDirectory when creating SmtpClient, then the mails stored in a location. So define a SmtpClient with dummy host name so those mails will be stored in the folder and never dispatched.

To test this, you just simply change the following line
if (true) intead of if (e.Error != null)

Now the mails are safely delivered or backed-up in case of server failure. 🙂

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: