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(“”, 587);           

            client.EnableSsl = true;

            MailAddress from = new MailAddress(“”, “[ 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(“”, “YourPassword”, “”);           

            client.Credentials = myCreds;





            catch (Exception ex)


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







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:
There is also a very good site containig basic and more advanced samples here:

Comments (11)

  1. Sanket B says:

    Interesting one.

    But isnt it more like advertising about google?

    How about something like using LIVE programatically?

  2. coryc13 says:

    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.

  3. Korayem says:

    I too have the same issue.


    I think you are referring to this

    If anyone has found a solution to this, can you please share it with me?

  4. DarkEol says:

    Thank you! Very useful code 🙂

  5. Milan says:

    Thanks a lot for this.This is really useful information. Thank u again….

  6. espinete says:


    how can Encrypt password of gmail ? thx

  7. Binod Kumar says:

    nice code frnd….keep it up!!!!!!!!!!!

  8. Husein Musawa says:

    Try Higlabo It works for me. I can send mail using using ssl. I wonder why only very few people know about this.

  9. Sekhar Babu says:

    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; in that –> contact us –>feedback form