October 2017 : Online course in ASP.NET MVC / Core. Conducted by Bipin Joshi. Read more...
Registration for October 2017 batch of ASP.NET MVC / Core online course has already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

Everything about sending Emails in ASP.NET 2.0- Part 1

Introduction

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:
  • MailMessage
  • MailAttachment
  • MailAddress
  • SmtpClient
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:
  • 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.
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:
  • 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 button.
MailMessage msg = new MailMessage();
msg.From = new MailAddress(TextBox1.Text);
string[] to = TextBox2.Text.Split(';');
foreach (string s in to)
{
msg.To.Add(new MailAddress(s));
}
if (TextBox3.Text != "")
{
string[] cc = TextBox3.Text.Split(';');
foreach (string s in cc)
{
msg.CC.Add(new MailAddress(s));
}
}
if (TextBox4.Text != "")
{
string[] bcc = TextBox4.Text.Split(';');
foreach (string s in bcc)
{
msg.Bcc.Add(new MailAddress(s));
}
}
msg.Subject = TextBox5.Text;
msg.Body = TextBox6.Text;
if (RadioButtonList1.SelectedValue == "HTML")
{
msg.IsBodyHtml = true;
}
else
{
msg.IsBodyHtml = false;
}

SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = true;
client.Send(msg);
lblMsg.Text = "Mail sent successfully!";
Here,
  • 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.

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. 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 Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 04 March 2006


Tags : ASP.NET Web Forms .NET Framework