Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am sending email its working fine now i want to save status of each email which could be success or fail

how could i do this.?

What I have tried:

public async static Task SendEmailAsync(string email, string subject, string message)
{
try
{
var _email = "xyz@gmail.com";
var _epass = "pwd";
var _displayname = "test mail";
MailMessage mymessage = new MailMessage();
mymessage.To.Add(email);
// mymessage.ReplyTo=email;
mymessage.From = new MailAddress(_email, _displayname);
mymessage.Subject = subject;
mymessage.Body = message;
mymessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

mymessage.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient()) {
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(_email,_epass);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.SendCompleted += (s, e) => { smtp.Dispose(); };
await smtp.SendMailAsync(mymessage);


}
}
catch (SmtpFailedRecipientsException ex){
throw ex;
}
}
Posted
Updated 20-Oct-19 7:32am
Comments
F-ES Sitecore 20-Oct-19 10:24am    
How do you define what is success and what is failure?
Richard Deeming 22-Oct-19 12:15pm    
catch (SmtpFailedRecipientsException ex){
    throw ex;
}
Don't do that. You've just destroyed the stack trace of the exception.

If you really need to re-throw an exception, just use throw; instead of throw ex;.

But in this case, since you're not doing anything with the exception you've caught, just remove the try..catch block altogether.

That depends on what you mean by "checking the status of the email".

You can get a status on the email being posted to your local email server to so it can be sent. You should really be using the synchronous version of SendMail since it appears you're trying to do this in a Task. That Task can make the operation Async separate from the call to SendMailAsync, and then you would be able to get the status back without using the Event handler.

After the message is successfully posted to the mail server specified in smtp.Host, it's impossible for you to know if the intended receiver actually got the email.
 
Share this answer
 
Comments
Richard Deeming 22-Oct-19 12:11pm    
"You should really be using the synchronous version of SendMail since it appears you're trying to do this in a Task."

Why? SendMailAsync[^] is specifically intended for use within an async method. The OP just needs to use a using block instead of the event handler to dispose of the SmtpClient.
Dave Kreskowiak 22-Oct-19 16:16pm    
My bad. I just looked at the code again. I thought he was spinning up a Task just to setup and await another Task returned by smtp.SendMailAsync.
Please update the catch block with the code given below,

catch (SmtpFailedRecipientsException ex)
       {
           for (int i = 0; i < ex.InnerExceptions.Length; i++)
           {
               SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
               if (status == SmtpStatusCode.MailboxBusy ||
                   status == SmtpStatusCode.MailboxUnavailable)
               {
                   Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                   System.Threading.Thread.Sleep(5000);
                   client.Send(message);
               }
               else
               {
                   Console.WriteLine("Failed to deliver message to {0}",
                       ex.InnerExceptions[i].FailedRecipient);
               }
           }
       }
 
Share this answer
 
Comments
Richard Deeming 22-Oct-19 12:13pm    
Thread.Sleep is not a good choice within an async method. You should use await Task.Delay(...); instead.

Also, those Console.WriteLine calls won't do much good within an ASP.NET application.

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