2

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();
    }

}
tereško
  • 58,060
  • 25
  • 98
  • 150
Learning
  • 19,469
  • 39
  • 180
  • 373

2 Answers2

11

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:


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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 1
    Agree, this is partial code problem is that when even email is not delivered it still wont raise any exception – Learning Jul 10 '12 at 05:09
  • 1
    Right, but provided everything goes through with the SMTP transaction, I'm not sure that there *is* any way for you to know. Just like when you send an email with your mail client. The only way you can know that delivery failed (after it hopped its way across the internet) is that you get a "delivery failed" reply email, usually from their ISP/mail provider – Jonathon Reinhart Jul 10 '12 at 05:14
  • Do you also check for error in the OnSendCompleted method? http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.onsendcompleted – Misha Jul 10 '12 at 05:16
  • 1
    @Misha that's only raised if you call [`SendAsync`](http://msdn.microsoft.com/en-us/library/ecwxwx34), not if you use the regular (synchronous) `Send` methods. With the sync methods, the same exception would be raised directly. – Jonathon Reinhart Jul 10 '12 at 05:26
  • @Jonathon, I have updated code in the question. Please see if that can help me to trap all error and delivery failure also which is important – Learning Jul 10 '12 at 05:32
  • 1
    `mailclient.UseDefaultCredentials = true;` set it to `false`.. you are providing your credentials – Niranjan Singh Jul 10 '12 at 05:36
  • @Niranjan, You are right brother i have made code changes to my actual code.. I am not sure how i missed that one. Thanks – Learning Jul 10 '12 at 06:37
  • 1
    @JonathonReinhart Ah sorry I missed / misread that part. – Eregrith Jul 17 '15 at 13:12
1

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

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Is there a way we can check the recipient email address is valid one i mean is it associate with the email server for example IamOnGmail@gmail.com / IamNotOnGmail@gmail.com – Learning Jul 10 '12 at 10:57
  • 1
    check [this](http://stackoverflow.com/questions/5873118/checking-if-an-email-address-exists) and [this](http://forums.asp.net/t/1500197.aspx/1) -- [link](http://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-address-exists-without-sending-an-email/) – Niranjan Singh Jul 10 '12 at 11:05