.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

sending auto generated mail through system.net.mail

Posted By:Nabin       Posted Date: September 03, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Sending Mail Message using System.Net.Mail namespace


Very often in our applications, we need to send automatic or user defined email to a single or multiple recipients. In Visual Studio .Net, it is very easy to achieve this by using System.Net.Mail namspace. This namspace contains a set of classes which are helpful to create SMTP Host and Mail Messages with or without attachments.

Set of classes

System.Net.Mail offers following classes

SmtpClient - is used to create SMTP host client and define port number (usually 25) to setup Host for sending emails
MailMessage - is used to create a new email message
MailAddress - is used to create email address

We shall go through one example below to understand the usage of each of the above class.


using System.Net.Mail;

Private void SendMail()
SmtpClient client = new SmtpClient("http://domainname.com", 25);

MailMessage msg = new MailMessage();

MailAddress from = new MailAddress("from@domain.com");
MailAddress to = new MailAddress("to@domain.com");
MailAddressCollection cc = new MailAddressCollection();

string subjectText = "This is subject line";
string bodyText = "Hi, This article is based on how to use System.Net.Mail namspace to send emails";

msg.From = from;
msg.Subject = subjectText;
msg.Body = bodyText;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;

Attachment att1 = new Attachment(@"f:\my folder\myfile.doc");


In the above example:-

SmtpClient class is used to create host and set port number which is used to send mails.

MailMessage class is primary class to create any message.

MailAddress class is used to create any valid email address.

The MailMessage.From can take only single address whereas MailAddress.To and MailAddress.CC can acquire multiple addresses. We can create collection to add multiple mail address and use them to assign/add it corresponding collections.

The subject and body are text based but if you want to include HTML tags into body text, you can do it but remember to set "IsBodyHtml" as True.

We can also set MailPriority as Normal, High or Low. We can also add attachments to our message object. To do this, we need to create object of Attachment class which takes "FileName" with path as an argument.

At the last, we can send this message by using client object.
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend