Thursday, January 24, 2008

Send email in asp.net web application using C#

Sending email in a web application is a common requirement. You might need this feature to send comments, feedback, newsletter, or tell a friend email etc. I’ll show the easiest way to send an email in Microsoft.net 2.0 and asp.net using C#.

First of all include following namespace in your asp.net web form.

using System.Net.Mail;

This namespace provide you different classes to specify various parameters to send an email. I’ll let you understand them one by one.

MailAddress class helps you create an email object. Remember in .net 2.0 you need to create an object for each email address. Or you can also you email address collection to specify multiple email addresses separated by commas.

We’re about to create from and to objects:

MailAddress objFrom = new MailAddress(“from email”,”from name”);
MailAddress objTo = new MailAddress(“to email”);

Now create a mail message, using from and to mail address objects:

MailMessage msgMail = new MailMessage(objFrom, objTo);

Let say, you want to send an email to multiple people then you’ll have to create a mail address collection object using MailAddressCollection class, and then add MailAddress objects in that collection.

MailAddressCollection objCCCollection = new MailAddressCollection();
MailAddress objCC = new MailAddress(“cc email1”);
objCCCollection.Add(objCC);

objCC = new MailAddress(“cc email2”);
objCCCollection.Add(objCC);

Now, add this email collection address in your mail message object:

msgMail.CC.Add(objCCCollection);

Similar is the case with BCC:

MailAddressCollection objBCCCollection = new MailAddressCollection();
MailAddress objBCC = new MailAddress(“bcc email”);

msgMail.Bcc.Add(objBCCCollection);

An email can be sent using two formats i.e. HTML or Text. You can specify this format as under:
msgMail.IsBodyHtml = true;

Now, set email subject and email body as under:

msgMail.Subject = sSubject;
msgMail.Body = sBody;


An SMTP server is used to send an email, you an either use your own server settings or your web host’s smtp mail settings. You need to check your hosting details or ask your hosting service provider about your SMTP settings.

SmtpClient objSMTP = new SmtpClient();

Follwing statement will assign credentials to this smtp object. The credentials should be a valid email account on your mail server.

objSMTP.Credentials = new System.Net.NetworkCredential("youusername", "yourpassword");

Sepecify your smtp server’s host and port number, by default it is 25:

objSMTP.Host = "mail.youdomain.com”
objSMTP.Port = 25;

Following statement will send your composed email to the recipients.

objSMTP.Send(msgMail);

1 comment:

carl said...

first, my name carl, it's nice to know you

i want to say thanks for this section. it's very important to finish my job.

But i want to ask!! when i use the code, i've got an error in "msgMail.CC.Add(objCCCollection);
". It says that cannot convert ... to string, how can i fix this problem????

it's very gratefull, if you can sent me the answer to my email : carl.santoso@gmail.com

thx a lot