Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
SmtpClientObj.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);

// SmtpClientObj.Send(Mail);
SmtpClientObj.SendAsync(Mail, "Message");

static void MailDeliveryComplete(object sender,
System.ComponentModel.AsyncCompletedEventArgs e)
    {
        //Console.WriteLine("Message \"{0}\".", e.UserState);

        if (e.Error != null)
        {
            //Console.WriteLine("Error sending email.");
            mailSent = false;
        }
        else if (e.Cancelled)
        {
            // Console.WriteLine("Sending of email cancelled.");
            mailSent = false;
        }
        else
        {
            //Console.WriteLine("Message sent.");
            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.
Posted
Updated 30-May-12 5:13am
v4
Comments
Raja Soosai 25-May-12 5:54am    
Please provide some sample codes
db7uk 30-May-12 7:49am    
I would use a switch statement instead of if else. I maybe wrong but you are stating that the mail has been sent in your last else block!
db7uk 30-May-12 7:54am    
ignore that. dumb answer.
Ed Nutting 30-May-12 14:09pm    
Hi there,
Are you sure there isn't another possibility for failure such as a "pending" status or something meaning the email is waiting to be sent, but hasn't actually been sent. This might result in your code thinking the email hasn't been sent when actually it is just waiting.

Not sure if this is correct, I've never tried myself, but it's at least worth checking,
Ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900