I m working on the email gateway. Different applications provide me email recipient and email content and i send email to those recipients. My application requires that i should try sending mails upto 6 times if mail sending fails for first time.
To track this i have used asynchrounous method to send emails and
MailDeliveryComplete
event handler . So that i could know that whether emails are sent or not. The
MailDeliveryComplete
event handler returns false if error occurs in sending mail else returns true.
So if it returns false i try to send email again and this i do for max 6 times.
But my problem is although the event handler returns false, sometimes it actually sends mail and since it has returned me false earlier i tried sending it again and hence the mail is delivered to recipient twice.
Can someone help me to handle this? Please tell me how can i decide whether mail is sent successfully. I searched on the Internet, it gave me option of asynchrounous method to sending emails, which had above issue.
My code is
SmtpClientObj.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
SmtpClientObj.SendAsync(Mail, "Message");
static void MailDeliveryComplete(object sender,
System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
mailSent = false;
}
else if (e.Cancelled)
{
mailSent = false;
}
else
{
mailSent = true;
}
}
Here mailSent is a global variable set to default value false and i m calling Asynchrounous method of smtp object to send mail which invokes
MailDeliveryComplete
event on async call and returns true or false.