Sending the mail message using your Gmail account and C#

Till the previous tutorial we have covered creating a mail message and storing it in a folder using the classes given to you by C#. In this tutorial we will learn how to actually send the email message using a real SMTP server. Here we will discuss how to use Gmail as your SMTP server and the settings you need to know to send your emails using your Gmail account.

First step - Set the delivery method to Network

The very first thing you need to do is to set the DeliveryMethod property of your STMPClient object to Network. This will make your SMTP client object connect with an external SMTP server to relay your emails.

Second step – Know the settings for using Gmail as your SMTP server

The great thing about all the major email companies like Gmail, Yahoo etc is that you can use them as SMTP servers and ask them to relay your emails under your username and password and they will do it happily. Suppose you wish to relay your emails using your Gmail account then you need to know the following settings

  1. SMTP Host name – This means the URL or the IP address of the SMTP server. To use Gmail you need to set this to
  2. Port number – This is the port on which the SMTP client object will communicate with Gmail. You need to set this to 587 when using Gmail
  3. Enable SSL – If your SMTP server uses a secure connection to communicate with your SMTP client object then set this value to True. Because Gmail does all the communication using secure connections you need to set this value to True for Gmail
  4. Your Gmail credentials – Gmail will only let you send emails if you have a Gmail account. So in order to send emails using your own Gmail account you need to provide your Gmail username and password to the SMTP client object.

Code example

Look at the code below to understand the C# code you need to write

static void Main(string[] args)
            // Create object of the mail message class      
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

            // Give a subject to your message      
            message.Subject = "My first email using C#";

            // Write the body content      
            message.Body = "This is where you write your email message.";

            // Add the sender email id      
            message.From = new System.Net.Mail.MailAddress("");

            // Add all email ids you want to send this mail to      

            // Create the SMTP Client object      
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();

            // Setting the delivery method to Network will mean that      
            // we want to use an external SMTP server for relaying      
            // our emails      
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            // The URL or the IP address of the SMTP server to connect with      
            smtpClient.Host = "";
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("", "your_password");

            // Finally, ask C# to deliver your email      

Read the comments in the code to understand what we have done here.

Important thing to keep in mind

One of the most important things to keep in mind when using your Gmail account to send emails is that the From address you use in the mail message object should be the same as the one you are using in the SMTP server credentials. If these addresses do not match Gmail will refuse to send your emails.

Per day limit on using Gmail

You should only consider using Gmail to send emails in your software project if you will be sending less than 500 emails per day. Gmail does not allow you to send more than 500 emails per day, and when you reach this limit your account is blocked this the next day.

Using HTML in your email body in C#

If you wish to send your email messages in the HTML format then you can simply set the IsBodyHtml property to True and write your emails message in the HTML format. Look at the code below

// Write the body content  
    message.Body = "<h1>Title</h1> <p>This is where you write your email message.</p>";  
    message.IsBodyHtml = true;

See how we have written the message body in HTML and set the IsBodyHtml property to true.  

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp