Sending Email from a C# application using Gmail account could not be easier. All It takes is writing few lines of code, and the magic appears, like in many C# implementations.
- add ‘using System.Net.Mail;’
- copy the code below (better - wrap it with a function), and change the relevant parameters. (username,password..)
Enjoy !
If you have any comment\improvement – drop me a line at the bottom.
class SendWithGmail
{
static void Main(string[] args)
{
MailMessage mail = new MailMessage();
mail.To.Add("DestinationUser@gmail.com");
mail.To.Add("DestinationUser@yahoo.com");
mail.From = new MailAddress("SourceUser@gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential
("SourceUser@gmail.com", "SourceUserPassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}