[Email] with Gmail

This code is a very simple one that you can use to make your C# (Cs or C sharp) application send you an email from any gmail adress. Just make a new gmailadress for this purpose and you’re off!

I do not recommend the GmailHelper library. It seems great, but i couldnt get it working on 2010 express since it had all kinds of old dependencies.
Sending email with c# is super easy, but it took me an hour to work it out, so as a note to myself and for your possibly convenience, we can use the following:

using System.Net.Mail;

  1. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
  2. System.Net.NetworkCredential cred = new System.Net.NetworkCredential("ACCOUNT@gmail.com", "PASSWORD");
  3. mail.To.Add("RECEIVER@RECEIVER.com");
  4. mail.Subject = "subj";
  5. mail.From = new System.Net.Mail.MailAddress("ACCOUNT@gmail.com");
  6. mail.IsBodyHtml = true;
  7. mail.Body = "message";
  8. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
  9. smtp.UseDefaultCredentials = false;
  10. smtp.EnableSsl = true;
  11. smtp.Credentials = cred;
  12. smtp.Port = 587;
  13. smtp.Send(mail);

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...

1 Response

  1. rogier says:

    Het werkt echt perfect en simpel, merci.