Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Need to catch the proper error for SMTP send email code

For some reason my program does not send email using SMTP server, it stops somewhere in this code, and I can't debug it, because it happens with the compiled .exe

public static void sendLog(MailMessage mail) {
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress(Constants.EMAIL_SENDER);
    mail.To.Add(Constants.EMAIL_RECEIVER);
    mail.Subject = Environment.UserName;
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new NetworkCredential(Constants.EMAIL_SENDER, Constants.EMAIL_SENDER_PASSWORD);
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);
}

what I need is to know what try/catch to use, and to handle the proper Exception to write in a .txt file?

Upvotes: 0

Views: 763

Answers (1)

Rohit
Rohit

Reputation: 10226

You should use proper SMTP Exception

  catch (SmtpException ex)
   {    
     // write to log file

    string msg = "Failure sending email"+ex.Message+" "+ex.StackTrace;

   } 

Upvotes: 2

Related Questions