Everything about sending Emails in ASP.NET 2.0- Part 3
Introduction
In
Part 1 and
Part 2
of this series we learnt to send mails from ASP.NET 2.0. Based on our knowledge
up till now we know how to send Text or HTML emails. But what if you want to
embed the images from the HTML formatted email along with the message itself?
This way the receiver need not have constant internet connection. In this
article we are going to learn just that.
The relevant classes
In order to embed images in your email message you need to use the following
classes:
- AlternateView: You can use the AlternateView class to create
email message in different formats. For example, Text as well as HTML. Each
instance of AlternateView class represents a single format.
- LinkedResource: The actual images inside the message are
represented by a class called LinkedResource. Normally it will be the image
that you want to embed but it can be any other resource also.
Example
Let's understand the working of above classes with an example. See the code
below:
AlternateView view=AlternateView.
CreateAlternateViewFromString(TextBox6.Text,null,"text/html");
LinkedResource resource = new
LinkedResource(Server.MapPath("myimage.jpg"));
resource.ContentId = "Image1";
view.LinkedResources.Add(resource);
msg.AlternateViews.Add(view);
Here,
- We created an instance of AlternateView class by calling its static
method - CreateAlternateViewFromString. This method has various overloads.
We use the one that accepts the body of the message, content encoding and
media type. We accept body from a TextBox. Since the encoding is ASCII we
pass null as the second parameter. Since our message is HTML we set the
media type as "text/HTML"
- The body text of the email must specify the embedded resource in the
form of "cid:<Id_Of_Resource>". For example, <img src="cid:Image1">. This ID
of the resource is called as its Content ID.
- Let's assume that we want to embed an image file called MyImage.jpg.
- We create an instance of LinkedResource class and pass the complete path
of the file to be embedded in the constructor.
- Then we set the ContentId property of LinkedResource class to the ID of
the resource (assumed to be Image1)
- We then add this LinkedResource to the LinkedResources collection of the
AlternateView
- Finally, we add the instance of AlternateView to the AlternateViews
collection of the mail message.
The other code for sending the email remains unchanged.
Running the sample web form
A complete working example is available for download along with the article.
The following is the complete code of our example.
protected void Button1_Click(object sender, EventArgs e)
{
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));
}
msg.Subject = TextBox5.Text;
msg.IsBodyHtml = true;
AlternateView view=AlternateView.CreateAlternateViewFromString
(TextBox6.Text,null,"text/html");
LinkedResource resource = new LinkedResource
(Server.MapPath("myimage.jpg"));
resource.ContentId = "Image1";
view.LinkedResources.Add(resource);
msg.AlternateViews.Add(view);
SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = true;
client.Send(msg);
lblMsg.Text = "Mail sent successfully!";
}
To test the code, run the web form and fill out From, To, Subject and Body.
Note that your body must contain a reference of the content ID. For example,
enter something like :
Here is my image : <img src=cid:Image1 />
Also ensure that there is an image file called MyImage.jpg in the virtual
root of your application. When you submit the web form you will find that the
email message contains the embedded image.
Summary
In this part we learnt how to send email messages with embedded images. In
the next part we will look into some configuration properties of the email host.