none
Outlook Email - Text or HTML format for Recepient RRS feed

  • Question

  • We have an outlook add in which sends some HTML in every email that goes out. We need to add mime type header to make sure that it renders the text template if recipient view settings are text email and not HTML.

    Any ideas on how it can be achieved.

    Thanks.

     
    Tuesday, March 24, 2015 4:40 PM

All replies

  • Basically we need to have two email templates - one for HTML view rendering and another one for Text view rendering so that right template renders at recipient.
    Tuesday, March 24, 2015 4:47 PM
  • Hello,

    You can use the PropertyAccessor class to set or get low-level properties. See Implementing a Form Region to Display Email Headers in Outlook 2010 for a sample code. 

    But I suppose the property value will be rewritten by the transport provider. 

    Tuesday, March 24, 2015 4:47 PM
  • The Outlook object model doesn't allow to specify templates. You can use the HTML body format for your emails. And if the recipients opens a message and replies using the Plain Text body format, the email will be displayed correctly even if it was sent using HTML. See the BodyFormat property for more information. 
    Tuesday, March 24, 2015 4:54 PM
  • You can add an extra MIME header on an outgoing message if you set a string property in the special PS_INTERNET_HEADERS namespace.

    E.g. MailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-My-Header"), "some value");

    when the message is received, the value will be either in the MIME transport headers property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x007D001F") or  promoted to a separate named property (see above) if the name/id mapping was already done in the current store or if the message is sent between 2 Exchange mailboxes in the same domain.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!





    Tuesday, March 24, 2015 5:19 PM
  • Thanks Dmitry. It seems like set property is something what we have to use but I am not sure how it will help. I am not able to understand that with customer header how email will be rendered in text or HTML format based on recipient settings? Wouldn't we need to set mime type for that and if yes, how can we do that? 
    Tuesday, March 24, 2015 5:25 PM
  • I thought you just needed something to be sent so that your code can decide on the receiving side what to do with it.

    You have no way of knowing how the recipient's mail client is configured. Unless you take over completely the submission process so that you generate your own MIME, the text MIME part will be generated from HTML/RTF set on the message being submitted.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, March 24, 2015 5:48 PM
  • Yes - we don't have any way to know what recipient settings are going to be. You got it right that we have to generate MIME in such a way that text MIME is generated for recipient with text settings and HTML MIME is generated for recipient with HTML Settings. Question is how to do that? Can you please post an example to do that?
    Tuesday, March 24, 2015 5:55 PM
  • I think we have to create a multipart email. We can easily do it using ASP.net but not sure how can we do this using Outlook Object Model. Need samples on that using outlook object model.

    Tuesday, March 24, 2015 6:04 PM
  • You cannot do that in Outlook unless you take over sending the message (figure out SMTP server settings, connect to the server, etc.).

    Try to make sure that whatever you do, your HTML gets converted to plain text gracefully.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, March 24, 2015 7:52 PM
  • Yes - we have control over sending and have access to mailietm object. Can we set the Content-Type header of mail item to multipart/alternative;boundary=--abcd--

    Is it possible to do that with MailItem object?

    Wednesday, March 25, 2015 11:55 AM
  • I think you are missing my point - you will be able to do that *only* if you cancel the message submission, programmatically convert the message to MIME format (where you can set the any MIME headers on any MIME part), programmatically figure out the SMTP server connection options (including password) and send the MIME generated by your code to the server. You can then programmatically create a fake sent message in the Sent Items folder.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 25, 2015 2:11 PM
  • so there is no way to set Content-Type header? I was thinking if we can set it using SetProperty method in ItemSend event?
    Wednesday, March 25, 2015 6:27 PM
  • What would the Content-Type be? Outlook at the very least will create 2 MIME parts: text/html and text/plain. Do you really need to change the type of one of the parts? Or just remove one MIME part?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 25, 2015 6:38 PM
  • I need to set it as:

    "Content-Type: multipart/alternative; boundary="--boundary_589_763b78ef-a0bf-46a3-8a91-6673fad70143"

    This I have copied and pasted from an email I have received in my inbox.


    Wednesday, March 25, 2015 7:27 PM
  • And how would it help you even if you could do that?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 25, 2015 8:25 PM
  • With content type set as multipart we can have 2 sections in email body - one for text email and another one for HTML email. Email client will render it on the basis of its view settings then.

    Please see below articles from where I referred this approach:

    http://www.enewsletterpro.com/articles/multi_part_mime_messages.asp 

    http://stackoverflow.com/questions/10631856/mime-type-to-satisfy-html-email-images-and-plain-text 

    http://kevinjmcmahon.net/articles/22/html-and-plain-text-multipart-email-/ 

    Wednesday, March 25, 2015 9:35 PM
  • That is exactly what Outlook does. The plain text version is generated by converting the HTML body to plain text.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 25, 2015 10:33 PM
  • yes but that is not elegant. For example, we have extension icon images (pdf, txt etc.). As it cannot render image, it shows the full URL of those images in the body which mess up the whole alignment and looks ugly. In plain text version of the email, we don't want to show these image URLs.
    Thursday, March 26, 2015 5:51 AM
  • Do you specify alt attribute for the img tag?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, March 26, 2015 6:03 AM
  • We do and it shows the alt attribute text as well as the image URL both.
    Thursday, March 26, 2015 9:17 AM
  • Any other ideas? I have tried setting up the content type to multipart using following code in itemsent event:

    mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/Content-Type", "multipart/alternative; boundary=--boundary_589_763b78ef-a0bf-46a3-8a91-6673fad70143");

    However when I receive the email, the header of the email shows the content type as:

    Content-Type: application/ms-tnef; name="winmail.dat"

    Please let me know if you have any suggestions to achieve it.

    Thanks.


    Thursday, March 26, 2015 9:02 PM
  • This really has nothing to do with the contents type. It has to do with how Outlook converts your HTML to plain text.

    Instead of referencing external images, add them as attachments and reference them in HTML as <img src="cid:xyz"> where xyz is the value of the PR_ATTACH_CONTENT_ID property set on the attachment using Attachment.PorpertyAccesor.SetProperty. PR_ATTACH_CONTENT_ID DASL name is http://schemas.microsoft.com/mapi/proptag/0x3712001F


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Thursday, March 26, 2015 10:51 PM
  • Thanks. If it is nothing to do with content type - is there any other way? Is it possible to have 2 different emails templates for plain text/HTML in same email? That is what multipart email is about?
    Friday, March 27, 2015 5:05 AM
  • I think you are missing my point - MIME is not a native Outlook format. MIME gets created when a message is about to be sent over SMTP. The MIME envelope gets created from various MAPI properties.

    Outlook stores plain text PR_BODY, PR_HTML and PR_RTF_COMPRESSED. Whenever you modify one of the body flavors, the other two get updated automatically by the store provider (if it supports HTML and RTF) when the message is saved. You have no control over how plain text body is generated when HTML is modified. The best you can do is make sure that your HTML gets converted to plain text in a reasonable fashion by tweaking your HTML.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 27, 2015 5:26 AM