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


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.


Let's understand the working of above classes with an example. See the code below:

AlternateView view=AlternateView.
LinkedResource resource = new 
resource.ContentId = "Image1"; 


  • 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
LinkedResource resource = new LinkedResource
resource.ContentId = "Image1";

SmtpClient client = new SmtpClient();
client.Host = "localhost";
client.UseDefaultCredentials = true;
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.


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.


Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ 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

Posted On : 25 March 2006

Tags : ASP.NET Web Forms .NET Framework