Click here to Skip to main content
Click here to Skip to main content

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

, 9 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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

 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)

Share

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
A highly 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 codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.
 
Over ten 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.
 
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
 
Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.
 
Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
 
Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:
 
1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.
4.Etc.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
Questionsmtpclient memory leak PinmemberMember 2903563-May-13 5:05 
AnswerRe: smtpclient memory leak PinmentorMd. Marufuzzaman3-May-13 5:18 
AnswerRe: smtpclient memory leak Pinmemberrobinvanderknaap20-Dec-13 4:00 
GeneralMy vote of 4 PinmemberA.J.Wegierski27-Mar-13 10:06 
QuestionHelpful... PinmemberMusthafa (Member 379898)25-Nov-12 15:20 
GeneralMy vote of 5 PinmemberRashed_10-Feb-12 23:10 
GeneralMy vote of 1 PinmemberEspiritu19-Oct-11 5:13 
GeneralMy vote of 1 PinmvpMark Nischalke12-Apr-10 11:53 
GeneralMy vote of 1 PinmemberNetDave2-Jan-10 5:28 
GeneralMy vote of 1 PinmemberGeorge Belletty9-Aug-09 22:18 
GeneralBenchmarks PinmvpN a v a n e e t h9-Aug-09 9:47 
GeneralRe: Benchmarks PingroupMd. Marufuzzaman9-Aug-09 10:11 
GeneralMy vote of 1 PinmemberMihai Guta4-Aug-09 22:44 
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman5-Aug-09 6:25 
GeneralMy vote of 1 PinmemberVMykyt4-Aug-09 21:12 
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman5-Aug-09 6:24 
GeneralRe: My vote of 1 PinmemberMoim Hossain27-Aug-09 21:43 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid