title: How to validate if email sent
body: <p>This is my code. I am using it to validate an email address.</p>
  
  <pre><code>using System;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using System.Web.Mvc;
  using System.Threading.Tasks;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using Vishwaprani.ContextClass;
  using Vishwaprani.Models;
  using System.Net.Mail;
  using System.Threading;
  namespace Vishwaprani.CustomValidator
  {
      public class EmailAddresValidationAttribute : ValidationAttribute
      {
          protected override ValidationResult IsValid(object value, ValidationContext validationContext)
          {
              if (value != null)
              {
                  string mailadress = Convert.ToString(value);
                  MailMessage email = new MailMessage(new MailAddress("emailvalidation123456@gmail.com", "Email validation"), new MailAddress(mailadress, "Validating email address"));
                  email.Subject = "email validation";
                     email.Body = @"Test for validation";
                   var client = new SmtpClient("smtp.gmail.com") ;
                  client.Port=587;
                  client.EnableSsl=true;
                  client.Credentials = new System.Net.NetworkCredential("emailvalidation123456", "U50h2#AP2XR@");
                  try
                  {
                       client.Send(email);
                  }
                   catch(SmtpFailedRecipientException ex)
                  {
                       if(ex.StatusCode==SmtpStatusCode.GeneralFailure||ex.StatusCode==SmtpStatusCode.LocalErrorInProcessing||ex.StatusCode==SmtpStatusCode.ServiceNotAvailable||ex.StatusCode==SmtpStatusCode.TransactionFailed)
                       {
                           Thread.Sleep(5000);
                           try
                           {
                               client.Send(email);
                           }
                           catch(SmtpFailedRecipientException exc)
                           {
                               if (exc.StatusCode == SmtpStatusCode.GeneralFailure || exc.StatusCode == SmtpStatusCode.LocalErrorInProcessing || exc.StatusCode == SmtpStatusCode.ServiceNotAvailable || exc.StatusCode == SmtpStatusCode.TransactionFailed)
                               {
                                   return new ValidationResult("There is a problem with your email address that can not be resolved. Please check it and try again later or enter another one.");
                               }
                           }
                       }
                       else if(ex.StatusCode==SmtpStatusCode.ExceededStorageAllocation||ex.StatusCode==SmtpStatusCode.InsufficientStorage)
                       {
                           return new ValidationResult("There is a storage issue with the email account. Please resolve that.");
                       }
                       else if(ex.StatusCode==SmtpStatusCode.MailboxBusy)
                       {
                           Thread.Sleep(5000);
                           try
                           {
                               client.Send(email);
                           }
                           catch(SmtpFailedRecipientException exct)
                           {
                               if (exct.StatusCode == SmtpStatusCode.MailboxBusy)
                               {
                                   return new ValidationResult("Mail box is busy two times try again later.");
                               }
                           }
                       }
                       else if(ex.StatusCode==SmtpStatusCode.MailboxUnavailable)
                       {
                           return new ValidationResult("Mail box is unavailable.");
                       }
                       else
                       {
                           return new ValidationResult("There is an error with the email address you provided. Please check it.");
                       }
                  }
                  return ValidationResult.Success;
               }
              else return null;
          }
      }
  }
  </code></pre>
  
  <p>The problem is when I am sending the mail to a wrong email address the code does not throw <code>SmtpFailedRecipientException</code> and goes to validationresult. Success. Anyway I am quite new to this SmtpClient stuff. Does anyone know this how to resolve this? . This is the faulty email address I used for validation: </p>
  
  <pre><code>dffhgjwghxjgwjhxg@gjh3egxejh.comedkjk. 
  </code></pre>
  
  <p>As you can see guys there is no way in the hell that kind of email address can be exist. </p>
  
  <p>Proof (checked with my one):</p>
  
  <p><a href="https://i.stack.imgur.com/IBPfW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IBPfW.png" alt="enter image description here"></a></p>