Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

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

3.74/5 (34 votes)
9 Aug 2009CPOL2 min read 143.2K   3K  
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)