Sending Simple Emails with Gmail

  1. using System.Net;  
  2. using System.Net.Mail;  
  3.   
  4. var fromAddress = new MailAddress("[email protected]""From Name");  
  5. var toAddress = new MailAddress("[email protected]""To Name");  
  6. const string fromPassword = "fromPassword";  
  7. const string subject = "Subject";  
  8. const string body = "Body";  
  9.   
  10. var smtp = new SmtpClient  
  11. {  
  12.     Host = "smtp.gmail.com",  
  13.     Port = 587,  
  14.     EnableSsl = true,  
  15.     DeliveryMethod = SmtpDeliveryMethod.Network,  
  16.     UseDefaultCredentials = false,  
  17.     Credentials = new NetworkCredential(fromAddress.Address, fromPassword)  
  18. };  
  19. using (var message = new MailMessage(fromAddress, toAddress)  
  20. {  
  21.     Subject = subject,  
  22.     Body = body  
  23. })  
  24. {  
  25.     smtp.Send(message);