Share via


Sending e-mail using SmtpClient and Gmail

The sample below used SmtpClient to send e-mail from your gmail account using your gmail username and password.

using System;

using System.Net;

using System.Net.Mail;

namespace GMailSample

{

    class SimpleSmtpSend

    {

        static void Main(string[] args)

        {

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

          client.EnableSsl = true;

            MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");

            MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");

          MailMessage message = new MailMessage(from, to);

            message.Body = "This is a test e-mail message sent using gmail as a relay server ";

            message.Subject = "Gmail test email with SSL and Credentials";

            NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");

            client.Credentials = myCreds;

            try

            {

                client.Send(message);

            }

            catch (Exception ex)

       {

                Console.WriteLine("Exception is:" + ex.ToString());

            }

            Console.WriteLine("Goodbye.");

        }

    }

}

 

What happens here is that SmtpClient sends your e-mail to a relay server (in this case the gmail web mail server) and then this relay server sends it to its specified destination. Of course you're not limited to just gmail. The relay server can be another mail server, or a SMTP server/service - you can use a third party one or set your own. I'll update the post to include more detail on this later.

 

You can easily expand this sample to a more elaborate one - with attachments, html content, embeded images, etc. For more detail please refer to our documentation on msdn2 here:

https://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
There is also a very good site containig basic and more advanced samples here:
https://www.systemnetmail.com/faq/3.aspx
https://www.systemnetmail.com/faq/4.aspx

Comments

  • Anonymous
    June 15, 2006
    I thought this was a pretty cool post: http://blogs.msdn.com/mariya/archive/2006/06/15/633007.aspx
    Not...

  • Anonymous
    June 16, 2006
    Interesting one.
    But isnt it more like advertising about google?
    How about something like using LIVE programatically?

  • Anonymous
    November 28, 2006
    First, thank you as this has been a very helpful starting point as I am just getting into .Net and C#. However, I have discovered a problem that seems to have been avoided in most discussions. You should be able to use port 465, but using your example (and others) fails to work on this port. I have tried this on my own server as well. I cannot authenticate on port 465. I did come across a post (forgive me for not rememberering where I saw it) that mentioned this is a problem with .Net and it's inability to authenticate using the Exchange structure - I'm paraphrasing. So, I would be interested if anyone has seen this same problem or if they have actually been able to use port 465 on any server (gmail or others) and if there is a patch for .Net to make this work correctly.

  • Anonymous
    March 22, 2007
    I too have the same issue. @coryc13: I think you are referring to this http://www.systemnetmail.com/faq/5.2.aspx If anyone has found a solution to this, can you please share it with me?

  • Anonymous
    June 08, 2007
    Thank you! Very useful code :-)

  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers-esp.com/446027-system-net-mail-para-cualquier

  • Anonymous
    November 24, 2010
    Thanks a lot for this.This is really useful information. Thank u again....

  • Anonymous
    April 13, 2011
    great, how can Encrypt password of gmail ? thx

  • Anonymous
    November 20, 2011
    nice code frnd....keep it up!!!!!!!!!!!

  • Anonymous
    March 03, 2012
    Try Higlabo system.net.smtp.smtpclient http://higlabo.codeplex.com. It works for me. I can send mail using smtp.gmail.com:465 using ssl. I wonder why only very few people know about this.

  • Anonymous
    August 23, 2012
    Hi dear, i have to get an email to my account when a user clicks send button in my site. How to get this done. Pls reply... My site is; http://www.panacya.in in that --> contact us -->feedback form