Saturday, August 17, 2013

Send Email with Embedded Images in C#

You might came across a scenario where we need to add Images as part of Email sent through code in HTML format. We can directly give the absolute url of the image and add the image reference in the email. But the when the server in which the Image is hosted goes Offline, we can’t get the image in our Email. To avoid this issue we can embed the Image in Email itself and then it will be available to the User even if the server is offline.

To Embed the Image in our Email we need to create an object for LinkedResource with our image path. Later this LinkedResource will be added to AlternateView of HTML type.
Each Image references should have one unique ID associated with the Image. So first while framing the HTML we will create the Unique ID and mention in “img src” as below,
<img src=”cid:YOUR UNIQUE ID”/>
Then you need use the same unique ID (which is mentioned in Image reference) for creating the Linked Resource object.
LinkedResource obj = new LinkedResource(“Physical path of the image”, "image/png");
Obj.ContentId = “YOUR UNIQUE ID; 
 
If your Image is hosted in any site and doesn’t exists in physical folder, you can get Stream from the Image and pass the Stream as Input while creating object for LinkedResource.

 
byte[] imgByte = null;
using (var webClient = new WebClient())
{
   webClient.UseDefaultCredentials = true;
   Uri uri = new Uri(url);
   imgByte = webClient.DownloadData(uri);
}
Stream memStream = new MemoryStream(img.ImgStream);
LinkedResource pic6 = new LinkedResource(memStream, "image/png");
pic6.ContentId = img.ImgGuid.ToString();
 

Once the LinkedResource object is created we need to add to an object of AlternateView of HTL type. Below is the full sample code snippet for embedding images (Check inline comments for more details).
 
// Creating Unique ID for adding image referrence
string GUId1 = Guid.NewGuid().ToString();               
 
// frame the HTML with the img tag and above unique id
string htmlBody = "<html><body><div><img src=\"cid:" + GUId1 + "\"></div></body></html>";
 
// Create alternateview object with Mime type HTML
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
                    (htmlBody, null, MediaTypeNames.Text.Html);
 
//Create object for Linked Resource with the Image physical path or Image Stream
LinkedResource pic1 = new LinkedResource(HttpContext.Current.Server.MapPath("/_layouts/Images/header.jpg"), "image/png");
 
// Provide the previously created Unique ID to associate the Image with the respective img src.
pic1.ContentId = GUId1;
 
//Add the Linked Resource to the AlternateView
avHtml.LinkedResources.Add(pic1);
 
string from = SPContext.Current.Web.Site.WebApplication.OutboundMailSenderAddress;
 
string smtpAddress = SPContext.Current.Web.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
 
// Assign SMTP address
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = smtpAddress;
 
MailMessage mailMessage = new MailMessage(from, toRecipients);
mailMessage.Subject = subject;
 
// Add the Alternate view with the Mail message
mailMessage.AlternateViews.Add(avHtml);
mailMessage.IsBodyHtml = true;
 
smtpClient.Send(mailMessage);
 
 

Happy Coding!

1 comment:

  1. thank you for this blog. You saved my 100 lines of code :)

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...