Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
3.74/5 (34 votes)
9 Aug 2009CPOL2 min read 141.2K   3K   37   17
A simple but effective way to send an email using SmtpClient class

Introduction

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.

Background

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:

MailMessage

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.

NetworkCredential

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.

SmtpClient

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

C#
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();

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

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

               objSmtpClient.Send(objMailMessage);
           }
           catch (Exception ex)
           { throw ex; }

           finally
           {
               objMailMessage = null;
               objSMTPUserInfo = null;
               objSmtpClient = null;
           }
       }

A member table of SmtpClient class is given below:

Class Description

Attachment

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

MailAddress

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

MailMessage

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.

Conclusion

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

History

  • 5th August 2009: Initial post

License

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



Comments and Discussions

 
Questionsmtpclient memory leak Pin
Member 2903563-May-13 5:05
Member 2903563-May-13 5:05 
AnswerRe: smtpclient memory leak Pin
Md. Marufuzzaman3-May-13 5:18
professionalMd. Marufuzzaman3-May-13 5:18 
AnswerRe: smtpclient memory leak Pin
Robin van der Knaap20-Dec-13 4:00
Robin van der Knaap20-Dec-13 4:00 
GeneralMy vote of 4 Pin
A.J.Wegierski27-Mar-13 10:06
A.J.Wegierski27-Mar-13 10:06 
QuestionHelpful... Pin
Musthafa (Member 379898)25-Nov-12 15:20
Musthafa (Member 379898)25-Nov-12 15:20 
GeneralMy vote of 5 Pin
Member 1051082210-Feb-12 23:10
professionalMember 1051082210-Feb-12 23:10 
GeneralMy vote of 1 Pin
Espiritu19-Oct-11 5:13
Espiritu19-Oct-11 5:13 
GeneralMy vote of 1 Pin
Not Active12-Apr-10 11:53
mentorNot Active12-Apr-10 11:53 
GeneralMy vote of 1 Pin
NetDave2-Jan-10 5:28
NetDave2-Jan-10 5:28 
GeneralMy vote of 1 Pin
George Belletty9-Aug-09 22:18
George Belletty9-Aug-09 22:18 
GeneralBenchmarks Pin
N a v a n e e t h9-Aug-09 9:47
N a v a n e e t h9-Aug-09 9:47 
GeneralRe: Benchmarks Pin
Md. Marufuzzaman9-Aug-09 10:11
professionalMd. Marufuzzaman9-Aug-09 10:11 
GeneralMy vote of 1 Pin
Mihai Guta4-Aug-09 22:44
Mihai Guta4-Aug-09 22:44 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman5-Aug-09 6:25
professionalMd. Marufuzzaman5-Aug-09 6:25 
GeneralMy vote of 1 Pin
VMykyt4-Aug-09 21:12
VMykyt4-Aug-09 21:12 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman5-Aug-09 6:24
professionalMd. Marufuzzaman5-Aug-09 6:24 
GeneralRe: My vote of 1 Pin
Moim Hossain27-Aug-09 21:43
Moim Hossain27-Aug-09 21:43 
Md. Marufuzzaman wrote:
i just tryed to optimize the send email function


I've really lost to figure out what optimization you did here!!
This code snippet is doing nothing special...there are houndreds example available online shows how to send mail..when you said "A simple but effective".. I got confused...why it's effective rather than the other code snippets found by google...?

Moim Hossain
R&D Project Manager
BlueCielo ECM Solutions BV

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.