Kaushik Dudhat

Kaushik Dudhat

  • 803
  • 1.6k
  • 93.7k

Can i track the email is sent or not in c# ?

Oct 19 2019 12:55 AM
I'm using smtp to send the email, if the receiver email address is wrong then the email is not send and no any exception found.
So anyone help me to find out the email was sent or not.?
 
My current code is like below
  1. public static void MailSend()  
  2.        {  
  3.            MailAddress from = new MailAddress("test@gmail.com");  
  4.            MailAddress to = new MailAddress("ja+nasd369874512e@cont225oso.com");  
  5.            MailMessage message = new MailMessage(from, to);  
  6.            // message.Subject = "Using the SmtpClient class.";  
  7.            message.Subject = "Using the SmtpClient class.";  
  8.            message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";  
  9.            SmtpClient client = new SmtpClient("smtp.gmail.com",587);  
  10.            client.Credentials = new NetworkCredential("KD369@gmail.com""kd");  
  11.            client.EnableSsl = true;  
  12.            Console.WriteLine("Sending an e-mail message to {0} using the SMTP host {1}.",to.Address, client.Host);  
  13.               
  14.            try  
  15.            {  
  16.                client.Send(message);  
  17.            }  
  18.            catch (SmtpFailedRecipientsException ex)  
  19.            {  
  20.                for (int i = 0; i < ex.InnerExceptions.Length; i++)  
  21.                {  
  22.                    SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;  
  23.                    if (status == SmtpStatusCode.MailboxBusy ||  
  24.                        status == SmtpStatusCode.MailboxUnavailable)  
  25.                    {  
  26.                        Console.WriteLine("Delivery failed - retrying in 5 seconds.");  
  27.                        System.Threading.Thread.Sleep(5000);  
  28.                        client.Send(message);  
  29.                    }  
  30.                    else  
  31.                    {  
  32.                        Console.WriteLine("Failed to deliver message to {0}",  
  33.                            ex.InnerExceptions[i].FailedRecipient);  
  34.                    }  
  35.                }  
  36.            }  
  37.            catch (Exception ex)  
  38.            {  
  39.                Console.WriteLine("Exception caught in RetryIfBusy(): {0}",  
  40.                        ex.ToString());  
  41.            }  
  42.        }  
  43.   
  44.        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)  
  45.        {  
  46.            // Get the unique identifier for this asynchronous operation.  
  47.            String token = (string)e.UserState;  
  48.   
  49.            if (e.Cancelled)  
  50.            {  
  51.                Console.WriteLine("[{0}] Send canceled.", token);  
  52.            }  
  53.            if (e.Error != null)  
  54.            {  
  55.                Console.WriteLine("[{0}] {1}", token, e.Error.ToString());  
  56.            }  
  57.            else  
  58.            {  
  59.                Console.WriteLine("Message sent.");  
  60.            }  
  61.        }  
 

Answers (1)