C# - Smtp With NetworkCredential

This code snippet shows how to send an email with network credentials.
  1. SmtpClient Smtp = new SmtpClient();
  2. MailMessage Message = new MailMessage();
  3.  
  4. try
  5. {
  6.         Smtp.Host = "";
  7.         Smtp.Port = "";
  8.         string SmtpUsername = "";
  9.         string SmtpPassword = "";
  10.         string MailFrom = "";
  11.         string MailFromName = "";
  12.  
  13.         Smtp.Credentials = new NetworkCredential(SmtpUsername, SmtpPassword);
  14.         MailAddress FromAddress = new MailAddress(MailFrom, MailFromName);
  15.  
  16.         Message.From = FromAddress;
  17.         Message.To.Add(MailFrom);
  18.         Message.Subject = "Subject";
  19.         Message.IsBodyHtml = true;
  20.         Message.Body = "Sample message";
  21.         Smtp.Send(Message);
  22. }
  23. catch (SmtpException SmtpEx)
  24. {
  25.         // Error
  26. }

No comments:

Post a Comment