Sending the mail message using the SMTPClient class in C#

In the previous tutorial we created a mail message using C# but did not send it. In this tutorial you will learn how to send the mail messages using C#. You will learn a few different ways to send emails using C#. In this tutorial we will teach you the easiest way to do it so that you understand the process.

You surely don’t have an SMTP server installed! So what to do??

At this point because you are very new to learning about sending emails using C# you definitely do not have an SMTP server installed and also you do not know the settings you need for your Google, Yahoo or other email account in order to use them to send your emails via C#. Then what should we do? The answer to this is very simple – we will learn the easiest way to understand how to ask C# to send the emails. Let’s explain how

Don’t send your emails, just store them in a folder

So that you understand the complete process we will first teach you how to store your emails in a folder after you create them using C#. But what’s the fun in doing so? The fun is that when you are in the development mode you generate a lot of test emails. You definitely do not want to send all those test emails to your friends or other people because otherwise they will be angry with you like anything! The best approach is to generate your emails and don’t send them, just store them in some folder. This approach has 2 advantages

  1. You will come to know how to write C# code to send emails.
  2. You will not have to research on the SMTP settings for Google, Yahoo etc to use them for sending emails.

After you complete this step we will teach you how to actually send emails to the end user using a real SMTP server.

SMTPClient class in C#

Just like C# gives you a class called MailMessage to create email messages it also gives you a class called SMTPClient to help you send these messages to the SMTP server. From there the SMTP server will do its job on its own. The first and the most important thing you need to tell your SMTPClient class is the way you want to deliver your message. There are three ways in which you can ask it to deliver your message

  1. SpecifiedPickupDirectory – This means that you want the SMTP client object to go and store the email message in some folder
  2. PickupDirectoryFromIis – This means that you want to relay the email using your IIS server and the email should be stored in the pickup directory of your IIS server. This is like saying that you want to send the emails using your own SMTP server.
  3. Network – This means that you want to handover the message to some other SMTP server like Google, Yahoo etc and want them to send your emails.

You can choose one of the delivery methods from the System.Net.Mail.SmtpDeliveryMethod enum which C# provides you.

Lets store our mail message in a folder

Look at the last three lines of code below

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      
        // Add people in the cc list     
        // Add people to the bcc list      
        // Create the SMTP Client object      
        System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
        // Let it know that we want the email to be      
        // stored in the directory of our choice      
        smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
        // Give the path of the pickup folder      
        // Make sure that this folder exists      
        smtpClient.PickupDirectoryLocation = @"C:\Emails\";
        // Finally, ask C# to deliver your email      

See how we are telling the SMTPClient on where to store the email. In the last line we are finally sending our message. This is what you will see in your C:Emails folder after you run this code

Sending emails using C#
A file got created and the extension of this file is .eml. If you have some email client software like Outlook installed on your computer you can double click and open this email to see how it looks. Below you can see how this email shows up in the outlook client on my computer

Sending emails using C#

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