Everything about sending Emails in ASP.NET 2.0- Part 1
Sending emails, automated or otherwise, is frequent requirement in web applications. Enquiry forms, orders, newsletters, alerts and notifications are some of the examples wherein emails are often used. In this article we are going to see what ASP.NET 2.0 (or .NET framework rather) has to offer for sending emails from our web applications.
Namespaces and classes
The emailing functionality of .NET resides in System.Net.Mail namespace. Users of ASP.NET 1.x will note a change here. In the previous versions this namespace used to be System.Web.Mail. Since emailing is not
specific to ASP.NET. It is in fact an implementation of SMTP protocol and hence Microsoft probably moved it under System.Net.Mail.The namespace provides the following important classes:
The MailMessage class represents an email message. The MailAttachment class represents an email attachment. The MailAddress class represents an email address that can be used in from, to, CC and BCC. Finally, SmtpClient implements all the necessary logic to send emails.
Composing email message
An email message to be sent is represented by a class called MailMessage. You need to set various properties of this class such as From, To, Subject and Body. The following list details some of the commonly used properties of this class:
Once you create and prepare the MailMessage instance you need to use SmtpClient class to actually send the message. The following list details some of the commonly used properties and methods of this class:
- From: Indicates the sender of the email in the form of MailAddress instance.
- To: Indicates direct recipients of the email in the form of MailAddress instances.
- CC: Indicates carbon copy recipients of the email in the form of MailAddress instances.
- Bcc: Indicates blind carbon copy recipients of the email in the form of MailAddress instances.
- Subject: Indicates the subject of the message.
- Body: Indicates the body of the message.
- IsBodyHtml: Indicates whether the message body is HTML or text.
- Host: Indicates the email server IP address or name.
- UseDefaultCredentials: Indicates whether we want the email server to authenticate us using the Windows login credentials.
- Send(): Sends the mail message.
Example - Sending simple email messages
In the following example we will build a simple email compose web form that
allows us to send emails. The complete source code is available along with this
article. The following code can be found in the Click event handler of Send Mail
MailMessage msg = new MailMessage();
msg.From = new MailAddress(TextBox1.Text);
string to = TextBox2.Text.Split(';');
foreach (string s in to)
if (TextBox3.Text != "")
string cc = TextBox3.Text.Split(';');
foreach (string s in cc)
if (TextBox4.Text != "")
string bcc = TextBox4.Text.Split(';');
foreach (string s in bcc)
msg.Subject = TextBox5.Text;
msg.Body = TextBox6.Text;
if (RadioButtonList1.SelectedValue == "HTML")
msg.IsBodyHtml = true;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = true;
lblMsg.Text = "Mail sent successfully!";
- We create an instance of MailMessage class and set various properties such as From, To, CC, Bcc, Subject and Body.
- We then create an instance of SmtpClient class
- We set Host property of SmtpClient class to localhost indicating that we are using IIS virtual SMTP server to send our emails.
- We set UseDefaultCredentials to true indicating that we want the email server to authenticate us using the Windows login credentials.
- Finally we call Send method of SmtpClient instancec passing the instance of MailMessage class.
is an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses
in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga
to interested individuals. To know more about him click here
Get connected : Facebook Twitter LinkedIn YouTube