none
Inserting an Outlook 2010 template programmatically within an email does not display its pictures RRS feed

  • Question

  • Hello everyone,

    Using VSTO with VS 2013, I created a tab within the ribbon that is only displayed when creating a new mail, replying or forwarding. This new tab has buttons which insert within the opened mail the content of previously created templates (.oft format).

    It works if the templates only contain text. If they contain pictures, they are not being displayed.

    Here is the bit of code that I made:

    private void AddTemplateToBody(string templateName)
    {
    	Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
    	Outlook.MailItem currentOpenedMail = inspector.CurrentItem as Outlook.MailItem;
    	try
    	{
    		Outlook.MailItem templateMail = Globals.ThisAddIn.Application.CreateItemFromTemplate(templatePath + templateName) as Outlook.MailItem;
    		// add at the beginning of the mail the content of the template
    		string templateBody = templateMail.HTMLBody;
    		templateMail.Close(Outlook.OlInspectorClose.olDiscard);
    		currentOpenedMail.HTMLBody = currentOpenedMail.HTMLBody.Insert(0, templateBody);
    	}
    	catch (Exception)
    	{
    		System.Windows.Forms.MessageBox.Show("Template " + templatePath + templateName + " not found!");
    		
    	}
    }

    Any suggestion?

    Please note that I tried to save the templates as .html files and parse their content via the following method, but the pictures were then displayed randomly.

    string templateHTMLContent = System.IO.File.ReadAllText(templatePath + templateName);
    currentOpenedMail.HTMLBody = currentOpenedMail.HTMLBody.Insert(0, templateHTMLContent);

    Thank you in advance, and my apologies if this something simple: I am in no way a dev, just an hobbyist :)

    Best regards,

    Nich

    Saturday, October 25, 2014 1:15 PM

All replies

  • Hello Nich,

    How did you specify images in the HTML markup?

    To add an embedded images which can be sent to others you need to specify the globally available URL (upload it to any web server) or embed an image into the email and specify its cid attribute as a source in the HTML markup. You may find the following links helpful:

    To add an embedded image to an HTML message

    cid, inline embedded images

    how to embed image in html body in c# into outlook mail

    Saturday, October 25, 2014 2:58 PM
  • Found a workaround: I am inserting the content of a Word document and it works.

    Would still appreciate if someone knew how to use Outlook templates instead of a Word document though!

    Saturday, October 25, 2014 2:58 PM
  • Please refer to my previous message.
    Saturday, October 25, 2014 2:59 PM
  • Hello Eugene and thank you for taking the time to reply.

    I did not touch the HTML source of any of the templates. I created a new mail, filled it with content (text+pictures), then chose "save as Outlook template".

    The problem is not about the recipients not being able to see the images, it's prior to even sending said mail that is created via C#: adding content of the template within a mail body, pictures wich can be seen when opening the templates alone, are not displayed.

    I already examined the HTML source of the created mail, and the cid would show up as something like this: src=\"cid:image001.jpg@01CFF05C.58F1C1D0

    I will do a bit more reading on the subject as this is not something I am very familiar with.

    Thank you for your time :)

    Saturday, October 25, 2014 3:25 PM
  • It looks like you need to add hidden attachments with "cid" attributes. Images are missed.

    Try using any Extended MAPI viewer such MFCMAPI or Outlook Spy for exploring attachments with ids mentioned in the HTML.

    Saturday, October 25, 2014 3:29 PM
  • Hi Nicolas,

    Base on my test, I can reproduce that issue.

    The reason is that you just read the template body and set it to current mail’s body. For this way, the current mail don’t have its attachments. (E.g. picture). So the picture won’t be displayed even though it has cid, because there isn’t that file with cid attribute.

    I suggest that you could create a new mail base on the template file directly or as Eugene said that add hidden attachments with cid attributes.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 28, 2014 6:43 AM
    Moderator