Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

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

Introduction

In the Part 1 of this series we learnt to send simple mails from ASP.NET 2.0. Continuing further we will see how to add attachments to the mail messages along with some more message options.

Attachments

Mail attachments are represented by a class called System.Net.Mail.Attachment (not MailAttachment as in .NET 1.x). One of the constructors of this class accepts the complete path of the file to be attached. One or more instances of Attachment class can be then added to the Attachments collection of MailMessage class.

In order to illustrate use of Attachment class we will modify our previous web form to include a File Upload control. The following code can be found in the Click event of the Send Mail button.

...
string filename=FileUpload1.PostedFile.FileName;
filename=Path.GetFileName(filename);
filename=Server.MapPath("~/attachments/" + filename);
FileUpload1.PostedFile.SaveAs(filename);
Attachment attach = new Attachment(filename);
msg.Attachments.Add(attach);
...

Whatever file you submit via FileUpload control bears a client side path. We first extract just the file name of the file being uploaded and then convert it into server side path. In our example we will store it in a folder called Attachments in the virtual root. In real world cases you may want to generate file name that is unique on the server. Then we call SaveAs method to save the file on the server. Finally we create an instance of Attachment class passing this saved file name.

More properties of MailMessage

We have already seen many of the properties of the MailMessage class. Let's see few more.

  • ReplyTo: Indicates the address where all replies should be directed to.
  • Priority: Indicates the priority of the mail message. This property is of type MailPriority and can have possible values of Normal, High and Low.
  • DeliveryNotificationOptions: Indicates the type of notification from the mail server that you want to receive. The property is of type DeliveryNotificationOptions and some of the values are OnSuccess, OnFailure and Delay.

Below is the complete code of the enhanced version of "Email Compose" web form:

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.ReplyTo = new MailAddress(TextBox7.Text);
msg.Subject = TextBox5.Text;
msg.Body = TextBox6.Text;
if (RadioButtonList1.SelectedValue == "HTML")
{
msg.IsBodyHtml = true;
}
else
{
msg.IsBodyHtml = false;
}
string filename=FileUpload1.PostedFile.FileName;
filename=Path.GetFileName(filename);
filename=Server.MapPath("~/attachments/" + filename);
FileUpload1.PostedFile.SaveAs(filename);
Attachment attach = new Attachment(filename);
msg.Attachments.Add(attach);
        
switch (RadioButtonList2.SelectedValue)
{
case "High":
msg.Priority = MailPriority.High;
break;
case "Normal":
msg.Priority = MailPriority.Normal;
break;
case "Low":
msg.Priority = MailPriority.Low;
break;
default:
msg.Priority = MailPriority.Normal;
break;
}
switch (RadioButtonList3.SelectedValue)
{
case "F":
msg.DeliveryNotificationOptions =
 DeliveryNotificationOptions.OnSuccess;
break;
case "S":
msg.DeliveryNotificationOptions =
 DeliveryNotificationOptions.OnFailure;
break;
case "D":
msg.DeliveryNotificationOptions =
 DeliveryNotificationOptions.Delay;
break;
}
SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = true;
client.Send(msg);
lblMsg.Text = "Mail sent successfully!";

That's it for this part! In the next part we are going to learn about embedding images in your email message so that users can view them while they are disconnected from internet.

 




Bipin Joshi is a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 18 Mar 2006



Tags : ASP.NET Web Forms .NET Framework