Send Email with Gmail SMTP using C# C# Send Mail

Today we be using c# to send email with Gmail SMTP. Gmail is probably the most used email service in the world.

SMTP stands for Simple mail transfer protocol.This protocol is used to send emails.

We will be creating a console application to send the mails.So lets step by step.

Step 1: Open visual studio and create a console application.

Step 2: Add new class file with name GEmail.cs and add below code in the same.

 public class GEmail
        public MailAddress SenderEmail { get; set; }
        public MailAddress RecipientEmail { get; set; }
        public string Subject { get; set; }
        public string Message { get; set; }
        public string GmailId { get; set; }
        public string GmailPwd { get; set; }

        public static void SendMessage(GEmail gm)
            MailMessage gmail = new MailMessage(gm.SenderEmail, gm.RecipientEmail);
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(gm.GmailId, gm.GmailPwd);

            client.Host = "";
            client.EnableSsl = true;
            gmail.Subject = gm.Subject;
            gmail.Body = gm.Message;

Step 3: Add below code in the Program class of the console application

 class Program
        static void Main(string[] args)
            GEmail gmail = new GEmail();
            Console.WriteLine("Enter Sender Email");
            gmail.SenderEmail = new MailAddress(Console.ReadLine());
            Console.WriteLine("Enter Recipient Email");
            gmail.RecipientEmail = new MailAddress(Console.ReadLine());
            Console.WriteLine("Enter Subject");
            gmail.Subject = Console.ReadLine();
            Console.WriteLine("Enter email message");
            gmail.Message = Console.ReadLine();
            Console.WriteLine("Enter gmail id");
            gmail.GmailId = Console.ReadLine();
            Console.WriteLine("Enter password");
            gmail.GmailPwd = Console.ReadLine();

                Console.WriteLine("Mail send");
            catch (Exception ex)
                Console.WriteLine("Exception occured "+ex.Message);

Step 4. Run the application by pressing Ctrl+F5. Enter all the details asked by the program like send email,recipient email etc. If the programs runs success fully and email is send the a message “Mail send” will appear in output. In case of any exception the relevant message will be shown using catch block.

You can download the source code : Download


Be the first to comment

Leave a Reply

Your email address will not be published.