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

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 8 and 2 and type the answer here:

Preview Your Comment.

 
Next entries »