C#.Net How To: Send email from gmail using c#

This article explains the steps to send email from gmail using c#. Sometimes you don’t want to rely on your host to send emails. What if you want to send an email using gmail account? Sending an email in C# is a very simple task and it would not take much time to implement the functionality in

Send email from Gmail using C#.Net
Send email from Gmail using C#.Net

In last article we learned to send an email in using C#. That article is very generic and only need to change the smtp server address, sender email id and password.

How to send email from gmail using c#

In this c# tutorial we are going to learn to send email from gmail using C#. Sending an email from C# is never been a big task. Please look at below code. Below given SendEmail is a method that accepts Recipients email id, subject of an email and body of an email. Call this method wherever required with passing three parameter values. Mostly you would like to call this method on send button. The most important thing is that you can place and call this method in any web application to send email in or winforms application or wpf applications.

   protected string SendEmail(string toAddress, string subject, string body)
     string result = “Message Sent Successfully..!!”;
     string senderID = SenderEmailID;// use sender’s email id here..
     const string senderPassword = Password; // sender password here…
       SmtpClient smtp = new SmtpClient
         Host =, // smtp server address here…
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
         Timeout = 30000,
       MailMessage message = new MailMessage(senderID, toAddress, subject, body);
     catch (Exception ex)
       result = “Error sending email.!!!”;
     return result;

You can use above method to send an email from gmail using c#. But what particular settings we used for gmail in above code? Below are the settings that you need to use –

  1. Smtp server address –
  2. Port – 587 or 465
  3. EnableSsl – true

Gmail always uses SSL/TLS authentication, hence you need to enable the SSL property on port 587 or 465 to send email in c#. As given above, the smtp address of the gmail is ““. The highlighted part on above code shows the SenderEmailID and password. You just need to replace them with your gmail id and password to send email using c#.

To get more insights on how to code, please read how to send an email in using C#.

In this c# tutorial we learned send email from gmail using c#. I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

More articles –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

10 Comments+ Add Comment

  • Thanks for the example Ashih… Even this might help… Have a look..

  • Simply Great!!!

  • Thanks

  • thanks

  • It was a Great help. Thanks a lot 🙂

  • Thank you so much!!! for the example

  • Hi,

    I am getting following error..

    The remote certificate is invalid according to the validation procedure.

    Thank you..

  • I got authentication error from above code.
    Plz help me as soon as possible.

  • i am facing this

    “The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at”

  • very usefull

Leave a comment