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

By , 9 Aug 2009
 

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)

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
Member
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
 
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
 
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
 
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
 
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionsmtpclient memory leakmemberMember 2903563 May '13 - 5:05 
AnswerRe: smtpclient memory leakmentorMd. Marufuzzaman3 May '13 - 5:18 
GeneralMy vote of 4memberA.J.Wegierski27 Mar '13 - 10:06 
QuestionHelpful...memberMusthafa (Member 379898)25 Nov '12 - 15:20 
GeneralMy vote of 5memberRashed_10 Feb '12 - 23:10 
GeneralMy vote of 1memberEspiritu19 Oct '11 - 5:13 
GeneralMy vote of 1mvpMark Nischalke12 Apr '10 - 11:53 
GeneralMy vote of 1memberNetDave2 Jan '10 - 5:28 
GeneralMy vote of 1memberGeorge Belletty9 Aug '09 - 22:18 
GeneralBenchmarksmvpN a v a n e e t h9 Aug '09 - 9:47 
GeneralRe: BenchmarksgroupMd. Marufuzzaman9 Aug '09 - 10:11 
GeneralMy vote of 1memberMihai Guta4 Aug '09 - 22:44 
GeneralRe: My vote of 1groupMd. Marufuzzaman5 Aug '09 - 6:25 
GeneralMy vote of 1memberVMykyt4 Aug '09 - 21:12 
GeneralRe: My vote of 1groupMd. Marufuzzaman5 Aug '09 - 6:24 
GeneralRe: My vote of 1memberMoim Hossain27 Aug '09 - 21:43 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid