C-Sharp: Send email from C# program

                   This post will help you to learn the concept for sending email from C# program. The example below is quite simple and easy to understand.  Before entering the code section let me give the basics of the classes and properties used here. This can used to send email to any email service provider like gmail, hotmail, ymail. If you wish to send email from other sevice provider other than google then you need to change the SMTP server name and port number accordingly.


This represents a email message to be sent. It has various properties to acheive this, like 'From', 'To', 'Body', 'Subject' which are self explanatory. It has even more properties like 'bcc', 'CC' for sending emails, the body section can also be HTML.


This class allows the application to send email by using Simple Mail Transfer Protocol. Basic properties of this class needs to be assigned values to acheive this,  'Port', 'Credentials, 'SMTP server name' are those

          Below I have created a simple console application in Visual Studio and added comment sections for easy understanding.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace SendEMail
    class Program
        static void Main(string[] args)
                MailMessage mail = new MailMessage();
                SmtpClient smtpC = new SmtpClient("smtp.gmail.com");
                //From address to send email
                mail.From = new MailAddress("from@gmail.com");
                //To address to send email
                mail.Body = "This is a test mail from C# program";
                mail.Subject = "TEST";
                smtpC.Port = 587;
                //Credentials for From address
                smtpC.Credentials = new System.Net.NetworkCredential("EmailID", "password");
                smtpC.EnableSsl = true;
                Console.WriteLine("Message sent successfully");
            catch (Exception e)

Hope this helps... Share your thoughts...



  1. The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets to send or receive data over the Internet. SMTP protocol is using for sending email from C#. C# use System.Net.Mail namespace for sending email.To send e-mail you need to configure SMTP server. If you don’t have any SMTP, you can use free SMTP server. You can also use your gmail account.

    More details with example: http://cybarlab.blogspot.com/2013/03/send-e-mail-using-c-sharp.html

    Hope it will help you.

    Tanks & best regard

  2. how to send mail with SMTP and proxy network having c#?