How to Send Emails Using C#

How to Send Emails Using C#

 This topic is a how to.
Please keep it as clear and simple as possible. Avoid speculative discussions as well as a deep dive into underlying mechanisms or related technologies.

Step By Step Procedure

Step 1:
Create a new project in Microsoft Visual Studio 2008 (File -> New -> Project -> Visual C# -> Console Application). Give Email_Sender name and also specify the location where to store the project.

Step 2:
Now add a new item to the project we just created (Project -> Add New Item -> Class). Specify a name to the class as ‘TNV_EmailHelper’. The code now looks like as follows.
The namespace used is
using System.Net.Mail;


class TNV_EmailHelper
{
    private SmtpClient _sClient = new SmtpClient();
    private System.Net.NetworkCredential _smtpCreds = null;
    private string _sendTo;
    private string _sendFrom;
    private string _subject;
    private string _body;

    //Constructor
    //change Host& Port names Based on SMTP server
    public TNV_EmailHelper(string host, int port)
    {
        _sClient.Host = host;
        _sClient.Port = port;
    }

    public void SendEmail(string sendFrom, // From EmailAddress
                          string password, // Password
                          string sendTo,   // To Email Address
                          string subject,  // Subject of email
                          string body)     // Boady of email
    {
        try
        {
            _sendFrom = sendFrom;
            _sendTo   = sendTo;
            _subject  = subject;
            _body     = body;

            _smtpCreds = new System.Net.NetworkCredential(
                                                    sendFrom,
                                                    password);
            _sClient.UseDefaultCredentials = false;
            _sClient.Credentials = _smtpCreds;
            _sClient.EnableSsl = true;

            MailMessage msg = BuildMessage();
            _sClient.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private MailMessage BuildMessage()
    {
        MailMessage msg = new MailMessage();
        MailAddress to = new MailAddress(_sendTo);
        MailAddress from = new MailAddress(_sendFrom);

        msg.Subject = _subject;
        msg.Body = _body;
        msg.From = from;
        msg.To.Add(to);

        return msg;
    }
}

Step 3:
Open Program.cs and in Main write the following lines to send the email

Program.cs

class Program
{
    static void Main(string[] args)
    {
        TNV_EmailHelper em = new TNV_EmailHelper(
                                 "Smtp.gmail.com", 587);
        em.SendEmail("from@gmail.com", "password",
                     "to@gmail.com",
                     "SUB: From Email Sender",
                     "BODY: Hello World !");
    }
}



Other Languages

This article is also available in the following languages:

Italian (it-IT)

 

Leave a Comment
  • Please add 6 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Ed Price - MSFT edited Revision 6. Comment: Updating Title per Guidelines

  • Luigi Bruno edited Revision 5. Comment: Fixed an error.

  • Luigi Bruno edited Revision 3. Comment: Added the "Other Languages" section. Added the "Multi Language Wiki Articles" tag.

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Luigi Bruno edited Revision 3. Comment: Added the "Other Languages" section. Added the "Multi Language Wiki Articles" tag.

  • Luigi Bruno edited Revision 5. Comment: Fixed an error.

  • Ed Price - MSFT edited Revision 6. Comment: Updating Title per Guidelines

Page 1 of 1 (3 items)