Sending Email from Gmail using ASP.NET

  1. protected void btnSendEmail_Click(object sender, EventArgs e)   
  2. {  
  3.     //Create Mail Message Object with content that you want to send with mail.    
  4.     System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(“[email protected]”, ”[email protected]”, “This is the mail subject”, “Just wanted to say Hello”);  
  5.   
  6.     MyMailMessage.IsBodyHtml = false;  
  7.   
  8.     //Proper Authentication Details need to be passed when sending email from gmail    
  9.     System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(“[email protected]”, “myPassword”);  
  10.   
  11.     /*Smtp Mail server of Gmail is “smpt.gmail.com” and it uses port no. 587  
  12. For different server like yahoo this details changes and you can  
  13. get it from respective server*/  
  14.     .  
  15.     System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(“smtp.gmail.com”, 587);  
  16.   
  17.     //Enable SSL    
  18.     mailClient.EnableSsl = true;  
  19.     mailClient.UseDefaultCredentials = false;  
  20.     mailClient.Credentials = mailAuthentication;  
  21.     mailClient.Send(MyMailMessage);  
  22. }