ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Send SMTP Emails using System.Net Class in C#
Author Name: Mudassar Khan Published Date: February 12, 2009
Filed Under :
C#.Net
Views: 12482

Here is a simple article that explains how to send emails using C# and also make use of threading in .Net to send emails to improve performance


Namespaces


The following Namespaces need to be imported in order to send emails


using System.Net;


using System.Net.Mail;



MailMessage Class Properties


Following are the required properties of the mail message class.

From – Sender’s email address

To – Recipient(s) Email Address

CC – Carbon Copies

BCC – Blind Carbon Copies

Subject – Subject of the Email 

Body – Body of the Email

IsBodyHtml – Specify whether body contains text or HTML mark up.

Attachments – Attachments if Any.

ReplyTo – ReplyTo Email address.



Creating a MailMessage Object


MailMessage mm = new MailMessage();


mm.From = new MailAddress("username@gmail.com");


mm.Subject = "Hello";


mm.Body = "<p>Body</p>";


mm.IsBodyHtml = true;



SMTP Class Properties


Following are the properties of the SMTP class.

Host – Your SMTP Server

EnableSsl – Specify whether you host accepts SSL Connections.

UseDefaultCredentials – Set to True inorder to allow authentication based on the Credentials of the Account used to send emails

Credentials – Valid login credentials for the SMTP server

Port – Port Number of the SMTP server



Below code describes how the above properties are applied


smtp.Host = "smtp.gmail.com";


smtp.EnableSsl = true;


System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();


NetworkCred.UserName = "username@gmail.com";


NetworkCred.Password = "xxxxxxxxx";


smtp.UseDefaultCredentials = true;


smtp.Credentials = NetworkCred;


smtp.Port = 587;

 



Send Email

The method Smtp.Send(..) is used to send the email. See the example below


smtp.Send(mm);

 


Send Email using Background Thread


Sending Email requires some amount of time. So many times user has to wait for the page to load while the mail is being sent. And if the server is not reachable the code will try to send email until timeout.

To avoid that the email can be sent using a background thread and the user can be displayed a message immediately without delay.

 

Import the following Namespace


using System.Threading;

 


Send email using a Background thread


Thread threadSendMails;


threadSendMails = new Thread(delegate()

{


    sendemail("username@gmail.com", "receiver@abc.com", "Hello", "<p>Body</p>", "C:\\MyDoc.txt", true);


});


threadSendMails.IsBackground = true;


threadSendMails.Start();



You can download the complete source here.

EmailUsingCSharp.zip (2.49 kb)


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

sandip said:
zip file of source code cannot be downloadbr thanks in advance
January 28, 2010  

Mudassar Khan said:
Reply To: sandip
The link is working now
January 28, 2010  

Pankaj Gupta said:
Thanksbr Nice code It is working properly.
April 13, 2010  

Vaibhav Maheshwari said:
I have created a page wherebr i have a textbox ( tb4)br I want whatever user enters in that textbox i get in my email id as bodybr of the email.br br Thanx in advance .
May 15, 2010  

Mudassar Khan said:
Reply To: Vaibhav Maheshwari
Refer my article
http://www.aspsnippets.com/Articles/Contact-Us-Form-with-Rich-TextBox-in-ASP.Net.aspx
May 15, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code