Click here to Skip to main content
13,289,107 members (53,855 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 4 Aug 2009

A Simple But Effective Way to Send an Email using SmtpClient Class

, 9 Aug 2009
Rate this:
Please Sign up or sign in to vote.
A simple but effective way to send an email using SmtpClient class


What does SMTP (pronounced as separate letters) actually stand for? It is short for Simple Mail Transfer Protocol, a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP server and the SMTP server when you configure your e-mail application.

In this article, we are going to create a simple function which will send an email.


It is a very common feature to allow a user to send feedback from a web application / Windows application. My concern was how to make it simple by using Microsoft Visual Studio .NET.

Using the Code

Now we are going to discuss the .NET library which I used for this function. Microsoft Visual Studio .NET provides the namespace:

  • System.Net
  • System.Net.Mail

We are going to use the following classes, which will be found in the above namespaces:

  • MailMessage
  • NetworkCredential
  • SmtpClient

Before starting, let's take a short look at the above three classes:


Represents an e-mail message that can be sent using the SmtpClient class. Instances of the MailMessage class are used to construct e-mail messages that are transmitted to an SMTP server for delivery using the SmtpClient class.

To specify the sender, recipient, and contents of an e-mail message, use the associated properties of the MailMessage class.

More details can be found at this link.


The NetworkCredential class is a base class that supplies credentials in password-based authentication schemes such as basic, digest, NTLM, and Kerberos. Classes that implement the ICredentials interface, such as the CredentialCache class, return NetworkCredential objects.

This class does not support public key-based authentication methods such as Secure Sockets Layer (SSL) client authentication.

More details can be found at this link.


Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).

The SmtpClient class is used to send e-mail to an SMTP server for delivery. The classes shown in the following table are used to construct e-mail messages that can be sent using the SmtpClient class.

More details can be found at this link.

Sample C# Code

void sendEmail(string strFrom
                            , string strTo
                            , string strSubject
                            , string strBody)
           /// Author, Md. Marufuzzaman
           /// Created,
           /// Local dependency, Microsoft .Net framework
           /// Description, Send an email using (SMTP).

           MailMessage objMailMessage = new MailMessage();
           System.Net.NetworkCredential objSMTPUserInfo =
               new System.Net.NetworkCredential();
           SmtpClient objSmtpClient = new SmtpClient();

               objMailMessage.From = new MailAddress(strFrom);
               objMailMessage.To.Add(new MailAddress(strTo));
               objMailMessage.Subject = strSubject;
               objMailMessage.Body = strBody;

               objSmtpClient = new SmtpClient(""); /// Server IP
               objSMTPUserInfo = new System.Net.NetworkCredential
               ("User name", "Password","Domain");
               objSmtpClient.Credentials = objSMTPUserInfo;
               objSmtpClient.UseDefaultCredentials = false;
               objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

           catch (Exception ex)
           { throw ex; }

               objMailMessage = null;
               objSMTPUserInfo = null;
               objSmtpClient = null;

A member table of SmtpClient class is given below:

Class Description


Represents file attachments. This class allows you to attach files, streams, or text to an e-mail message.


Represents the e-mail address of the sender and recipients.


Represents an e-mail message.

Points of Interest

NetworkCredential class does not support public key-based authentication methods such as Secure Sockets Layer (SSL) client authentication.


This is a very simple and easy method. I hope that it might be helpful to you. Enjoy!


  • 5th August 2009: Initial post


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


About the Author

Md. Marufuzzaman
Bangladesh Bangladesh
A well experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by and also selected as a mentor of Published over 100 technical articles in various software development resource sites (i.e.,, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over twelve years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.I have also more than two years’ of strong experience in mobile-VAS (platform development).

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint.Honest, believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, Research, MIS, Data Analytics, Data Mining, BI, SaaS platform base application development, Large scale Win32 Form/Web based business software solutions, Security, Enterprise applications development, integration, etc.

Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12/14 , Oracle version 10/11, MySql version 5.1, 5.5, 5.7, Visual C#, VB.NET, ASP.NET, API, MVC, WebAPI , WPF, WCF, PHP, Microsoft Power BI, SPSS, PS2, R, Add-In, Visual Basic etc.

.Net UI component: Telerik, DevExpress, Ext.Net.
Scripting language: JavaScript, AngularJS, node.JS.
Source control / Subversion: Git,SVN.
Development methodologies: Agile,RAD.
Project Management / Issues Tracking Tools: JIRA, ClockingIt.

You may also be interested in...


Comments and Discussions

Questionsmtpclient memory leak Pin
Member 2903563-May-13 6:05
memberMember 2903563-May-13 6:05 
AnswerRe: smtpclient memory leak Pin
Md. Marufuzzaman3-May-13 6:18
mentorMd. Marufuzzaman3-May-13 6:18 
AnswerRe: smtpclient memory leak Pin
robinvanderknaap20-Dec-13 5:00
memberrobinvanderknaap20-Dec-13 5:00 
GeneralMy vote of 4 Pin
A.J.Wegierski27-Mar-13 11:06
memberA.J.Wegierski27-Mar-13 11:06 
QuestionHelpful... Pin
Musthafa (Member 379898)25-Nov-12 16:20
memberMusthafa (Member 379898)25-Nov-12 16:20 
GeneralMy vote of 5 Pin
Rashed_11-Feb-12 0:10
memberRashed_11-Feb-12 0:10 
GeneralMy vote of 1 Pin
Espiritu19-Oct-11 6:13
memberEspiritu19-Oct-11 6:13 
GeneralMy vote of 1 Pin
Mark Nischalke12-Apr-10 12:53
mvpMark Nischalke12-Apr-10 12:53 
GeneralMy vote of 1 Pin
NetDave2-Jan-10 6:28
memberNetDave2-Jan-10 6:28 
GeneralMy vote of 1 Pin
George Belletty9-Aug-09 23:18
memberGeorge Belletty9-Aug-09 23:18 
GeneralBenchmarks Pin
N a v a n e e t h9-Aug-09 10:47
mvpN a v a n e e t h9-Aug-09 10:47 
GeneralRe: Benchmarks Pin
Md. Marufuzzaman9-Aug-09 11:11
groupMd. Marufuzzaman9-Aug-09 11:11 
GeneralMy vote of 1 Pin
Mihai Guta4-Aug-09 23:44
memberMihai Guta4-Aug-09 23:44 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman5-Aug-09 7:25
groupMd. Marufuzzaman5-Aug-09 7:25 
GeneralMy vote of 1 Pin
VMykyt4-Aug-09 22:12
memberVMykyt4-Aug-09 22:12 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman5-Aug-09 7:24
groupMd. Marufuzzaman5-Aug-09 7:24 
GeneralRe: My vote of 1 Pin
Moim Hossain27-Aug-09 22:43
memberMoim Hossain27-Aug-09 22:43 

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
Web02 | 2.8.171207.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid