Images are not displaying in Emails

Hi Aspose Team,

I am using Aspose.Email tool to save and upload outlook .msg file in our website.

When those Emails are coming for display in browser, any image in the mail are not displaying.

Can you please check. I am copying the HtmlBody of the email object in our page for display.

Uploading one email which contains image for sample. Please guide.

Thanks

Amit Burnwal

Hi Amit,


Thanks for writing to Aspose.Email support team.

Copying the HtmlBody will not serve the purpose as it does not contain the image data but contains links to the images in the body. Could you please give a try to the following code which loads the MSG file and then saves the body of the message into memory stream. This memory stream contains complete data of images as well.

You can verify it by saving this stream to file and view it in explorer.

MailMessage msg = MailMessage.Load(“Spread the smiles - Weekly mail(#51) Fern and the Bamboo - Moral to be learned_2.msg”, MessageFormat.Msg);
MemoryStream msgHtmlBody = new MemoryStream();
msg.Save(msgHtmlBody, MessageFormat.Mht, MailMessageSaveOptions.None);

//For confirmation of “msgHtmlBody” contents, open the following file in browser
msg.Save(“File.mht”, MessageFormat.Mht, MailMessageSaveOptions.None);

Hi Kashif Iqbal,

Thank you for your response.

I tried your code and when i try to open the .mht file in IE it is also opening with image. But my problem is that i need to assign the complete body of the Email (including the image) to the Text property of a asp.net Label.

Some thing like below.

MailMessage mail = MailMessage.Load("C:\\Documents and Settings\\493285\\My Documents\\test mail\\File.mht", MessageFormat.Mht);

emailLabel.Text = mail.HtmlBody

How could i assign the memory stream or any property of Email object to the Text property of asp.net Label.

Please suggest. I need to display the email body in a collapsible panel inside a grid on my page.

thanks

Amit Burnwal

Hi Aspose Team,

Please reply on my query.

Thanks

Amit Burnwal

Hi Amit,

The Html cannot store the image data itself, but it contains the path of the image in src property of tag.

If the Html contains embedded images the HTML source is like below

Each of the embedded image is contained in MailMessage.LinkedResource collection. You can get the source of the image from LinkedResource.ContentId. To display embedded images, you may use the following approach:

  1. Save each linked resource to disk with name = LinkedResource.ContentId. The path of the saved image should be same as the web page where you want to display the image
  2. Modify the tag and replace all occurrences of “cid” to “”

Example Code:

MailMessage msg = MailMessage.Load("TestHtml.msg");

// Save all embedded images

foreach (LinkedResource res in msg.LinkedResources)

{

    res.Save(res.ContentId);

}

String strHtmlContents = msg.HtmlBody.Replace("cid:", "");

//Save the HTML contents to file

System.IO.File.WriteAllText("Test11.html", strHtmlContents);

//Or use it as text of your label control

The resultant html should work fine with the ASP.Net Panel control, as is also mentioned at this LINK about displaying HTML contents in a panel control. Please give it a try at your end and let us know your feedback.

Hi Kashif Iqbal,

The way you suggested is going to work. But can't I put the linked resources (image files) in some other folder like images. Putting those with aspx files are going to mess up the folder structure of my application. I tried saving them in some image folder and giving reference of these saved images in page but it was not working.

I want to understand how this linked resources are connected to email object. if possible can you please explain a little.

Please suggest

Thanks

Amit Burnwal

Hi Amit,


It seems that you are confusing the concepts here. As I mentioned earlier, HTML doesn’t save image data in it, but links to the images using tag. The src attribute in tag refers to the path where images are stored.

In Aspose.Email, linked resources are referred in message.htmlbody using cid:ImageName. In order to refer to these in the html body, you need to replace the cid:ImageName with path:ImageName, so that the saved HTML file can refer to the correct path of the image that is extracted and stored from the Linked Resources. I have attached a sample ASP.NET application here that reads the input file, saves the linked resources to Images folder, and then saves the HTML file with cid: replaced with the path where images are stored. The basic concept is to replace with correct path to the images. Please have a look at it for your reference and let us know your feedback.


String path = System.AppDomain.CurrentDomain.BaseDirectory;


String imagesPath = path + “Images\”;


String srcMsgPath = path + “MsgFiles\”;


MailMessage msg = MailMessage.Load(srcMsgPath + “TestHtml.msg”);


// Save all embedded images

foreach (LinkedResource res in msg.LinkedResources)

{

res.Save(imagesPath + res.ContentId);

}


//Replace ContentsID with path to images folder

String strHtmlContents = msg.HtmlBody.Replace(“cid:”, “images\”);


//Save the HTML contents to file

System.IO.File.WriteAllText(path + “Test11.html”, strHtmlContents);