Send Email Using Gmail SMTP

Here, we will see how can we send email using our Gmail SMTP in c# .NET. Generally, for sending an email from one place to another two parties are required -- first is the sender and the second is the receiver. We will use the Mail Message class of  .NET to send an email. For this, we will require a few details:
  1. Sender Email Address
  2. Sender Password
  3. Receiver Email Address
  4. Port Number
  5. EnableSSL property

And most important before we start coding we need to import two namespaces to  access the MailMessage Class:

  • using System.Net;
  • using System.Net.Mail;

Note

EnableSSL property generally specifies whether SSL is used to access the specified SMTP mail server or not. Please go through the piece of code to send an email as given below. Nowadays Gmail has become more secure due to various security reasons, so while sending email using mail message class we get the Authentication Error also. Please follow the steps given below to change some settings in Sender Gmail account to get rid of that error.

  1. using System.Net;  
  2. using System.Net.Mail;  
  3. namespace SendMail {  
  4.     class Program {  
  5.         static string smtpAddress = "smtp.gmail.com";  
  6.         static int portNumber = 587;  
  7.         static bool enableSSL = true;  
  8.         static string emailFromAddress = "[email protected]"//Sender Email Address  
  9.         static string password = "Abc@123$%^"//Sender Password  
  10.         static string emailToAddress = "[email protected]"//Receiver Email Address  
  11.         static string subject = "Hello";  
  12.         static string body = "Hello, This is Email sending test using gmail.";  
  13.         static void Main(string[] args) {  
  14.             SendEmail();  
  15.         }  
  16.         public static void SendEmail() {  
  17.             using(MailMessage mail = new MailMessage()) {  
  18.                 mail.From = new MailAddress(emailFromAddress);  
  19.                 mail.To.Add(emailToAddress);  
  20.                 mail.Subject = subject;  
  21.                 mail.Body = body;  
  22.                 mail.IsBodyHtml = true;  
  23.                 //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment  
  24.                 using(SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) {  
  25.                     smtp.Credentials = new NetworkCredential(emailFromAddress, password);  
  26.                     smtp.EnableSsl = enableSSL;  
  27.                     smtp.Send(mail);  
  28.                 }  
  29.             }  
  30.         }  
  31.     }  
C#

Follow the below steps to get rid of this error.
  1. Sign in to your Gmail account and go to settings.
  2. Go to Account and Import and click on other Google Account settings.

    C#

  3. Then click on 

    C#

  4. Turn this ON (Allow less secure app)

    C# 
Now, run the code and send an email to you dear one .... happy coding :)