How to check if email was delivered using C# MailM

2019-03-09 14:33发布

问题:

This question already has an answer here:

  • How to confirm that mail has been delivered or not? 6 answers

I am using below code to send email it works fine most of the time & during test we found sometimes it doesn't deliver email. How can i alter this code to check the email delivery status or font any other failure.

        public static void SendEmail(string to, string subject, string message, bool isHtml)
        {
            try
            {
            var mail = new MailMessage();

            // Set the to and from addresses.
            // The from address must be your GMail account
            mail.From = new MailAddress("noreplyXYZ@gmail.com");
            mail.To.Add(new MailAddress(to));

            // Define the message
            mail.Subject = subject;
            mail.IsBodyHtml = isHtml;
            mail.Body = message;

            // Create a new Smpt Client using Google's servers
            var mailclient = new SmtpClient();
            mailclient.Host = "smtp.gmail.com";//ForGmail
            mailclient.Port = 587; //ForGmail


            // This is the critical part, you must enable SSL
            mailclient.EnableSsl = true;//ForGmail
            //mailclient.EnableSsl = false;
            mailclient.UseDefaultCredentials = true;

            // Specify your authentication details
            mailclient.Credentials = new System.Net.NetworkCredential("noreplyXYZ@gmail.com", "xxxx123");//ForGmail
            mailclient.Send(mail);
            mailclient.Dispose();
    }
                    catch (Exception ex)
                    {
    throw ex;
                        }
    }

I know SMTP is responsible for sending email & it is not possible to delivery status but is their a way around to check the status of the email delivery

UPDATED CODE (is this correct)

public static void SendEmail(string to, string subject, string message, bool isHtml)
{
    var mail = new MailMessage();

    // Set the to and from addresses.
    // The from address must be your GMail account
    mail.From = new MailAddress("noreplyXYZ@gmail.com");
    mail.To.Add(new MailAddress(to));

    // Define the message
    mail.Subject = subject;
    mail.IsBodyHtml = isHtml;
    mail.Body = message;

    // Create a new Smpt Client using Google's servers
    var mailclient = new SmtpClient();
    mailclient.Host = "smtp.gmail.com";//ForGmail
    mailclient.Port = 587; //ForGmail

    mailclient.EnableSsl = true;//ForGmail
    //mailclient.EnableSsl = false;
    mailclient.UseDefaultCredentials = true;

    // Specify your authentication details
    mailclient.Credentials = new System.Net.NetworkCredential("noreplyXYZ@gmail.com", "xxxx123");//ForGmail
    mailclient.Send(mail);
    mailclient.Dispose();
    try
    {
        mailclient.Send(mail);
        mailclient.Dispose();
    }
    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);
                mailclient.Send(mail);
            }
            else
            {
                //  Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient);
                throw ex;
            }
        }
    }
    catch (Exception ex)
    {
        //  Console.WriteLine("Exception caught in RetryIfBusy(): {0}",ex.ToString());
        throw ex;
    }
    finally
    {
        mailclient.Dispose();
    }

}

回答1:

Well, you have the entire body of code wrapped in a try block with an empty catch block. So, if the message fails to send for whatever reason, you will have no idea because your function will simply return.

If you look at the MSDN documentation for SmtpClient.Send you'll see that there are a number of different exceptions it can throw for various reasons. A couple interesting ones:

  • SmtpException
  • SmtpFailedRecipientsException

A couple of notes after your update:

You probably don't mean to do this:

mailclient.Send(mail);
mailclient.Dispose();
try
{
    mailclient.Send(mail);
    mailclient.Dispose();
}

You're disposing mailclient before trying to use it again.

using

MailMessage and SmtpClient both implement IDisposable, so it would be best practice (and easiest) to put them in a using block:

using (var mail = new MailMessage())
using (var mailclient = new SmtpClient())
{
    // ...
}

Then you won't have to worry about calling Dispose() in your finally blocks (you may not need them at all then).

throw

You're probably aware, but there's no point in:

catch (Exception ex)
{
    throw ex; 
}

foreach

for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
    SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
    // ... 
}

Can be re-written as:

foreach (var innerEx in ex.InnerExceptions)
{
    var status = innerEx.StatusCode;
}

Thread.Sleep()

If this code is user-facing, you probably don't really want to do this, as it is going to cause the page to hang for 5 seconds waiting to send. In my opinion, you shouldn't handle sending mail directly in the web page code anyway, you should queue it up for a background task to send. But that's an entirely different issue.

Just a few things to help make you a better C# coder.



回答2:

It sounds like you're asking if there's a way in real time to check to see if your user got the message. If so, I would recommend that you don't pursue that path. While most times email delivery seems to be instantaneous, it could be held up for any length of time prior to being delivered to the recipient's mailbox.

I suggest you to go through following asp.net forum links:
SMTP server and email FAQ
Delivery Notification Not Working when Sending Emails
Best Practice to do implement checking if the email sent

Note: There is no reliable way to find out if a message was indeed delivered.

There are another SO thread already availalbe that you have asked:
How to check MailMessage was delivered in .NET?
ASP.NET MVC How to determine if the e-mail didn't reach the receiver