.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to send an Html Email with Embedded Image?

Posted By:Jean Paul       Posted Date: October 18, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Html Email Embedded Image

How to send an Html Email with Embedded Image?


In this small article I am explaining how to send an html email with embedded image.  The steps in this are following:


  1. Create the Html Body with tag
  2. Create the Images
  3. Attach the Image resource using cid
  4. Send Email


The steps are explained below:


1.      Create the Html Body


The following could be our html email body.


Test Body


In the above code we can see the img tag.  We can have multiple image tag on the image will be placed in the particular location.


Another important point to note is the cid.  It represents the content id of the resource.  The resources added can be assigned a content id.


The html email code will be as shown below:

MailMessage message = new MailMessage(AdminEmail, RecipientEmail);

message.IsBodyHtml = true;

message.Subject = "Test Subject";




2.      Create the Images


For the demo purpose, I have created one image which will be included in the html email.




3.      Attach the Image resource using cid


Now that we need to add the image in the email using cid.  The following code do this.


AlternateView view = AlternateView.CreateAlternateViewFromString(Message, null, MediaTypeNames.Text.Html);

LinkedResource resource = new LinkedResource(ImagePath);

resource.ContentId = "id1";


From the above code we can see that the id1 is the content Id.  A new class named AlternateView is used to specify the image.  The resource is added to the email message using the following line of code:




4.      Send Email


Now we can use the Send() method to send the email.  The received email will be looking like the following:





Hope you enjoyed it.  The associated code is attached with the article. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend