Click here to Skip to main content
13,351,954 members (87,665 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 14 Sep 2013

Email Sending Library

, 14 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Provides ready to use library for sending email using SMTP


This is a small library which will enable your website / web application to send emails instantly. This is fully tested code and provides quick integration.


Today’s application is not just software but a solution. In this solution, we always had a need to communicate to the user. There are various ways of communicating, email is one of them and is widely used. All developers must have felt the need to integrate an email sending option in their solution. If you Google, you will find various samples, tutorials and articles of doing so. They impart good amount of knowledge. However, developing email sending feature for your project will take a good amount of time.

Using the Code

The library uses a single method SendMail for performing the email sending feature. There are five steps involved in the sending feature.

Step One

//Step 1: Create Mail client, Mail message, Mail addresses.
SmtpClient MailClient = new SmtpClient();
MailMessage Email = new MailMessage();
MailAddress FromMailAddress = new MailAddress(EmailDetails.EmailFrom, EmailDetails.EmailDisplayName);
MailAddress ReplyToMailAddress = new MailAddress(EmailDetails.EmailFrom, EmailDetails.EmailDisplayName);
MailAddress[] ToMailAddress = new MailAddress[EmailDetails.EmailTo.Length]; 

The System.Net.Mail namespace provides four classes namely SmtpClient, MailMessage, MailAddress and Attachment which will be used for sending email. In this step, we create instances of SmtpClient, MailMessage and MailAddress. Here we are creating two instances of MailAddress class to allow specify Sender's Email information and Reply to Email information separately. We have created an array of MailAddress for Recipient's Email to allow multiple email addresses.

Step Two

//Step 2: Set the Mail client credentials and enable SSL.
MailClient.Host = ConfigurationManager.AppSettings["EmailHost"];
MailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]);
MailClient.Credentials = new NetworkCredential
(ConfigurationManager.AppSettings["EmailUsername"], ConfigurationManager.AppSettings["EmailPassword"]);
MailClient.EnableSsl = true;  

In this step, email server details with credentials are set. All these are taking from <appSettings>. The following settings are added to the web.config file from where the library will get the information.

Settings (web.config)
    <add key="EmailFrom" value=""/>
    <add key="EmailUsername" value=""/>
    <add key="EmailPassword" value="tester1234"/>
    <add key="EmailHost" value=""/> 
    <add key="EmailPort" value="587"/>

Since the library takes the settings from web.config, no changes to the code are required.

Note: The setting name should exactly be the same as mentioned in the settings block above.

Step Three

//Step 3: Set all Email options.
Email.From = FromMailAddress;
Email.ReplyTo = ReplyToMailAddress;
Email.Subject = EmailDetails.EmailSubject;
Email.Body = EmailDetails.EmailContents;
Email.IsBodyHtml = true;
for (int i = 0; i < ToMailAddress.Length; i++)
                    string EmailDisplayName = string.IsNullOrEmpty
                    (EmailDetails.EmailToDisplayName[i]) ? "Recipient " + 
                    i.ToString() : EmailDetails.EmailToDisplayName[i];
                    ToMailAddress[i] = new MailAddress(EmailDetails.EmailTo[i], EmailDisplayName);

Now all the options required for sending email are set the Sender, Recipients, Subject and Body. Since there can be multiple Recipients, a for loop all of them to MailMessage.

Step Four

//Step 4: Set all the attachments.
if (EmailDetails.EmailAttachments!=null)
	Attachment EmailAttachment = null;
	for (int i = 0; i < EmailDetails.EmailAttachments.Length; i++)
		EmailAttachment = new Attachment(EmailDetails.EmailAttachments[i]);

Now all attachments are added to the MailMessage. If there are no attachments, pass null and the attachment adding step is skipped.

Step Five

//Step 5: Send Email.
Status = true; 

This is the final step where email is sent. The Send method doesn't return anything. In case it fails, it throws an exception. The exception is captured and false status is returned to the calling method. A boolean value is returned indicating whether sending was success or failure.

The attached sample contains a sample web form with the method call and also the library source code. This library can be used by adding reference to the library(DLL) in other projects.

To send email, simply call SendEmail method as mentioned below.

Method Call

ResultLBL.Text = SLEmail.SendEmail(ToTBX.Text, "Tester", 
SubjectTBX.Text, BodyTBX.Text, null, true).ToString(); 


  • Recipient's Email
  • Recipient's Name
  • Subject
  • Body of Email
  • Attachments
  • Whether the Email service uses SSL?

Points of Interest

We always look at software reusability. I used to copy paste this code in all my applications and would ideally change the Email Server Credentials and other related details in the code. I learned how I can create a library(DLL) which can be reused and made pluggable by reading the Email Server Credentials and other related details from a config file rather than editing the code.

Using class object as parameter to pass values which avoids changes to the method definition and method call. Also, we need not worry about the sequence. You can simply add members to class when you need to add parameters.


I would welcome suggestions from all of you. Also, I am planning to enhance this library with more capabilities.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shailesh Pisat
Software Developer (Senior)
India India
I am a senior software developer. I work for a web development company as team lead. Most of the solutions developed by myself are for web using DotNet Framework but do have keen interest in developing for mobile app as well. At work I am responsible to provide solutions to clients and work with dynamic teams developing for web and mobile. I have been closely working with teams and mentoring them developing in technologies like ASP.Net and PHP for Web and Objective C, Android SDK and cross platform Mobile app.

I like to try my hands-on various other web and mobile development technologies. My current interests apart from ASP.Net (WebForms, MVC and DotNet Core) is for NodeJS, Angular, React and Typescript for developing Web and Mobile Apps. I have been constantly exploring new technologies and implementations for CI/CD, Source Control & Versioning and Automation. Also, looking forward to work on Bots, Machine learning, IoT, VR & AR.

I use tools to do my work and suit my workflows. I was made aware of blogging during my training at Aptech and has been constantly working on having blogs for myself and my work. Including this blog there are few more links of my online presence mentioned below.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
AliceYeh15-Sep-13 18:31
memberAliceYeh15-Sep-13 18:31 
GeneralRe: My vote of 2 Pin
AliceYeh15-Sep-13 23:44
memberAliceYeh15-Sep-13 23:44 
Your article is good. But it's basic concept we can learn in MSDN. More depth will be better. Big Grin | :-D
GeneralRe: My vote of 2 Pin
Shailesh Pisat18-Sep-13 7:18
professionalShailesh Pisat18-Sep-13 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 14 Sep 2013
Article Copyright 2013 by Shailesh Pisat
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid