Go to home page mail me! RSS Feed

Sending email via GMail smtp server

Friday, August 24, 2007 10:00 AM

If for some reason you need to send mail via the Google GMail SMTP server, here is how you do it. Keep in mind that the connection has to be secured and it's using an unusual port. If you have a firewall, you may want to do some re-configuration.

 

   1: int port = 587;
   2: string userName = "myemail@gmail.com";
   3: string password = "password";
   4:  
   5: try
   6: {
   7:     MailMessage message = new MailMessage();
   8:  
   9:     message.From = new MailAddress("myemail@gmail.com", "Rydal Williams");
  10:     message.To.Add(new MailAddress("anotheremail@hotmail.com", "Rydal Williams"));
  11:     message.Subject = "Test email via GMail smtp server.";
  12:     message.Body = "Put email message here.";
  13:  
  14:     SmtpClient smtp = new SmtpClient("smtp.gmail.com", port);
  15:     smtp.Credentials = new System.Net.NetworkCredential(userName, password);
  16:     smtp.EnableSsl = true;
  17:     smtp.Send(message);
  18:  
  19:     Console.WriteLine("Message sent successfully!");
  20: }
  21: catch (Exception ex)
  22: {
  23:     Console.WriteLine("Failed to send message because " + ex.Message);
  24: }
  25: finally
  26: {
  27:     Console.ReadKey();
  28: } 

Your Comments.

  • # re: Sending email via GMail smtp server

    GravatarThis article really helped a lot

    Left by pooja at 11/15/2007 6:33 AM
  • # re: Sending email via GMail sasdsmtp server

    Gravatarasd

    Left by asd at 8/30/2009 4:40 AM
  • # re: Sending email via GMail smtp server

    GravatarHi,
    Code is working but when user receive mail. He/ she receive mail as name use for NetworkCredential not message.From ..............
    than what is message.From used for, how to solve this prob..........?????????

    Left by ferhan at 11/16/2009 12:45 PM
  • # suzanne.wilber@gmail.com

    GravatarThere were alcohol on the premises this weekend.Client left her child with another client for 3 hours outside premises.

    Left by client at 2/1/2010 8:26 AM

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 1 and 7 and type the answer here:

Preview Your Comment.

 
Next entries »