title: How to know if Email failed to reach its receivers
body: <p>I am sending email using the following utility class, if receiver foo@foo.com is not exist or the email never reach him for one reason or another, I want my application to be notified.
  <br /><br />
  It works only when the smtp client failed to connect to the smtp server, I got an exception in this case, otherwise, the only way to know if email fail to reach the client is by checking client account.</p>
  
  <pre><code> public static class EmailUtil
      {
          /// &lt;summary&gt;
          /// Fires exception if string is null or empty
          /// &lt;/summary&gt;
          /// &lt;param name="param"&gt;param value&lt;/param&gt;
          /// &lt;param name="paramName"&gt;is the parameter name to be shown in the exception message&lt;/param&gt;
          private static void CheckStringParam(string parameter, string paramName)
          {
              if (String.IsNullOrEmpty(parameter))
              {
                  throw new ArgumentException(String.Format("{0} can't be null or empty", paramName));
              }
          }
  
          public static void SendEmail(EmailArgument emailArg)
          {
              CheckStringParam(emailArg.FromEmail, "emailArg.FromEmail");
              CheckStringParam(emailArg.Subject, "emailArg.Subject");
              CheckStringParam(emailArg.Body, "emailArg.Body");
              string body = emailArg.Body;
  
              MailMessage mailMsg = new MailMessage();
              mailMsg.From = new MailAddress(emailArg.FromEmail);
  
              foreach(string recipient in emailArg.ToEmails)
              {
                  if (String.IsNullOrEmpty(recipient))
                  {
                      throw new ArgumentException("One of the values in the emailArg.ToEmails array is null or empty");
                  }
  
                  mailMsg.To.Add(new MailAddress(recipient));
              }
  
              mailMsg.IsBodyHtml = emailArg.IsHtml;
  
              if (emailArg.PutHtmlTags)
              {
                  body = String.Format("{0}" + body + "{1}", "&lt;HTML&gt;&lt;Body&gt;", "&lt;/Body&gt;&lt;/HTML&gt;");
              }
  
              mailMsg.Body = body;
              mailMsg.BodyEncoding = emailArg.BodyEncoding;
  
              // Get client info from the config file , it is tested with Web.config, need to be tested with App.config
              SMTPConfiguration smtpConfig = (SMTPConfiguration)System.Configuration.ConfigurationManager.GetSection("SMTPConfigurationGroup/SMTPServer");
  
              SmtpClient client = new SmtpClient(smtpConfig.Host, smtpConfig.Port);
              client.EnableSsl = smtpConfig.EnableSSL;
              client.Credentials = new System.Net.NetworkCredential(smtpConfig.Username, smtpConfig.Password);
  
  
  
              // The notifications of failure are sent only to the client email.
              // Exceptions are not guranteed to fire if the Receiver is invalid; for example gmail smtp server will fire exception only when receiver@gmail.com is not there. 
              // Exceptions will be fired if the server itself timeout or does not responding (due to connection or port problem, extra).
              mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  
              client.Send(mailMsg);            
          }
  
  
      }
  </code></pre>