Sending a sitecore media item in email body using System.Net.Mail.SMTPClient

I'm trying to send a sitecore media item in the email body. I'm using System.Net.Mail.SMTPClientT for sending the mail. The code is given below

Sitecore.Data.Items.Item cartItem = Sitecore.Data.Database.GetDatabase("master").GetItem(cartItemId);
Sitecore.Data.Fields.ImageField productImageField = ((Sitecore.Data.Fields.ImageField)cartItem.Fields["Large Product Image"]);
mailBody = @"<html><body>";
mailBody += "<h2>"+cartItem.Fields["Product Title"]+"</h2>";
mailBody += cartItem.Fields["Author Name"];
mailBody += "<img src=cid:purchasedProductID>" + "<br/>";
mailBody += cartItem.Fields["Product Unit Price Name"];
mailBody +="</body></html>";
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailBody, null, "text/html");
//Add Image
LinkedResource purchasedProductImage = new LinkedResource(Server.MapPath(MediaManager.GetMediaUrl(productImageField.MediaItem)));
purchasedProductImage.ContentId = "purchasedProductID";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(purchasedProductImage);

//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
mailSender = cartItem.Fields["Sender Email ID"].Value;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(mailSender, cartItem.Fields["Sender Email Password"].Value);
smtp.EnableSsl = true;
mail.From=new MailAddress(mailSender);
mail.To.Add(mailReceiver);
mail.Subject = cartItem.Fields["Email Subject"].Value;
mail.Body = mailBody;
smtp.Send(mail);

I get an error at this code statement below:

LinkedResource purchasedProductImage = new LinkedResource(Server.MapPath(MediaManager.GetMediaUrl(productImageField.MediaItem)));

What I understand from the error thrown, is that the error is caused because the media is searched in a wrong path. See the error screenshot below:

Could not find a part of the path 'C:\Inetpub\wwwroot\Allegheny\Website\-\media\User-Defined\JustDeals\Books-Images\Professional-Books\Entrance-Exam-Preparation-Books\understanding-physics-for-jee-main.ashx'.

Without using Server.MapPath,

i.e., LinkedResource purchasedProductImage = new LinkedResource(MediaManager.GetMediaUrl(productImageField.MediaItem));

I got this error being searched in my C: drive (without Webroot folder)

Could not find a part of the path 'c:\-\media\User-Defined\JustDeals\Books-Images\Professional-Books\Entrance-Exam-Preparation-Books\understanding-physics-for-jee-main.ashx'.

I just want to send a sitecore media item (which is in an item's field) to send in the email body. Any fix to this error or alternate method available dev's...Pls let me know...Hoping to hear the solution soon...Thanks!

  • Try this:

    Get the media item from the ImageField:

    var mediaItem=productImageField.MediaItem;


    And instead of created the LinkedResource with the url, use the stream that comes from the media item:


    LinkedResource purchasedProductImage = new LinkedResource(mediaItem.GetMediaStream());


    Let me know if it works.
  • Now, I got an answer to my question. It is helpful to other developers.

    Sitecore.Data.Items.MediaItem image = new Sitecore.Data.Items.MediaItem(imageField.MediaItem); string mimetype=image.MimeType; Stream stream = MediaManager.GetMedia(image).GetStream().Stream;

    In this stage, I got an image as a stream

    Next, I created the email

    //Create two views, one text, one HTML. var bodyMail="Test Message";

    //Add image to HTML version System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(stream ); imageResource.ContentId = "HDIImage"; System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString("" + textbody, null, "text/html");

    htmlView.LinkedResources.Add(imageResource); //Add two views to message. msg.AlternateViews.Add(htmlView);