locked
Confirm that an email has been sent RRS feed

  • Question

  • I'm trying to send an email with attachment and I want to confirm that it was send successfully. I'm using "SendMailAsync" and "SendComplete", but I don't get any change in the "AsyncCompleteEventArgs". Can someone point me in the right direction?

    private async void SendEmail(string FileToAttach)
    {
      MailMessage mail = new MailMessage();
      SmtpClient SmtpServer = new SmtpClient(emailSettings.SMTPServer);
      mail.From = new MailAddress("no-reply@test.com");
      mail.To.Add("test@test.com");
      mail.Subject = "Test Email";
      mail.Body = "Testing";
    
      System.Net.Mail.Attachment attachment;
      attachment = new System.Net.Mail.Attachment(FileToAttach);
      mail.Attachments.Add(attachment);
      SmtpServer.Port = 25;
      SmtpServer.EnableSsl = false;
    
      SmtpServer.SendCompleted += SendCompletedCallback;
      await SmtpServer.SendMailAsync(mail);
    }
    
    private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
      while (e.UserState != null)
      {
        Thread.Sleep(250);
        Application.DoEvents();
      }
    
      MessageBox.Show("Will never reach this messagebox...");
    }

    Monday, June 13, 2016 4:35 PM

Answers

  • Hi Sam1973,

    If you want to check if the email has been sent successfully, using try-catch statement as suggested above is a useful and convenient method to achieve your goal. The following similar thread talks about this and you could have a look.

    http://stackoverflow.com/questions/11406889/how-to-check-if-email-was-delivered-using-c-sharp-mailmessage

    >>” I do have a try/catch to catch application errors, just stripped it off for the website.”

    Do you have any during implementing the try/catch to handle the mail sending? If so, you’d better show more things to clarify the detailed error message.

    Besides, if william’s suggestion is helpful to you, you could mark it as answer so that to help other community members who have the same issue.

    Best Regards,

    Albert Zhang

    • Marked as answer by DotNet Wang Tuesday, June 28, 2016 1:55 AM
    Tuesday, June 14, 2016 8:44 AM
  • Well, after some time of trying to deliver it (hours to half-days) the mailserver will likely (not guranteed) generate a "could not deliver) mail. These might be intercepted and parsed, but not within this piece of code - you simply can not wait that long.
    Also some antispam measures will generate fake version of these messages (the idea being to get that Mailadress of the spamlists).

    If you want to verify that the user has access to a mail adress the classical method is: Send a mail with a link to click/a code to enter into your formular. And only progress if the the last send link has been clicked/code been entered. This is a database design and application flow problem, not a page design question.

    • Edited by Christopher84 Wednesday, June 15, 2016 6:42 AM
    • Marked as answer by DotNet Wang Tuesday, June 28, 2016 1:55 AM
    Wednesday, June 15, 2016 6:41 AM

All replies

  • The short answer - you can't. Since you are using SMTP- its not possible to tell whether the delivery succeeded or not. Read the SMTP specification. Mail is routed while being delivered, so:

    1. There's no guarantee your message is sent as soon as you call smtp.Send().
    2. Since SMTP is routed, you can't be sure that some node on the route won't fail with delivery to uplink.

    At the very least your code should be in a try / catch block.

    try
    {
        mail.Send(msg);
    }
    catch (SmtpFailedRecipientException ex)
    {
    
    }

    The Send method will raise an Exception if there's a problem sending. But as I said, beyond getting that message to the SMTP server, there's no way to know if it makes it to the destination from there.


    william xifaras


    Monday, June 13, 2016 7:39 PM
  • That is what I was afraid of. I do get error message from the SMTP server, but I was hoping to catch that in my application. I guess it is just “send and forget”.

    I do have a try/catch to catch application errors, just stripped it off for the website.

    Thanks for your reply.

    Monday, June 13, 2016 9:06 PM
  • Hi Sam1973,

    If you want to check if the email has been sent successfully, using try-catch statement as suggested above is a useful and convenient method to achieve your goal. The following similar thread talks about this and you could have a look.

    http://stackoverflow.com/questions/11406889/how-to-check-if-email-was-delivered-using-c-sharp-mailmessage

    >>” I do have a try/catch to catch application errors, just stripped it off for the website.”

    Do you have any during implementing the try/catch to handle the mail sending? If so, you’d better show more things to clarify the detailed error message.

    Besides, if william’s suggestion is helpful to you, you could mark it as answer so that to help other community members who have the same issue.

    Best Regards,

    Albert Zhang

    • Marked as answer by DotNet Wang Tuesday, June 28, 2016 1:55 AM
    Tuesday, June 14, 2016 8:44 AM
  • Well, after some time of trying to deliver it (hours to half-days) the mailserver will likely (not guranteed) generate a "could not deliver) mail. These might be intercepted and parsed, but not within this piece of code - you simply can not wait that long.
    Also some antispam measures will generate fake version of these messages (the idea being to get that Mailadress of the spamlists).

    If you want to verify that the user has access to a mail adress the classical method is: Send a mail with a link to click/a code to enter into your formular. And only progress if the the last send link has been clicked/code been entered. This is a database design and application flow problem, not a page design question.

    • Edited by Christopher84 Wednesday, June 15, 2016 6:42 AM
    • Marked as answer by DotNet Wang Tuesday, June 28, 2016 1:55 AM
    Wednesday, June 15, 2016 6:41 AM