How do I send HTML (multipart/alternative) from Exchange Web Services (2010 SP1)

Tony picture Tony · Jan 13, 2011 · Viewed 18k times · Source

This issue came to light as I was trying to send an HTML message with inline images (attachments) through EWS. However I have backed off to the simplest form and found that I cannot even send HTML messages.

I am using C#, .NET 4, EWS API 1.1, Exchange Server 2010 SP1.

Here is the core of my code:

ExchangeConnection = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ExchangeConnection.Credentials = new WebCredentials(Username, Password, Domain);
ExchangeConnection.Url = new Uri(ServiceURI);

string MailBody = @"<html><head></head><body>
    This is a <b>test</b> <strong>HTML</strong> <em>message</em>.<br><br>
    Did it work?</body></html>";

NewMessage = new EmailMessage(ExchangeConnection);
NewMessage.Subject = "Test HTML Message #001";
NewMessage.Body = MailBody;
NewMessage.Body.BodyType = BodyType.HTML;
NewMessage.ToRecipients.Add("[email protected]");
NewMessage.Send();

Using that code I receive a message with the following content:

Subject: Test HTML Message #001
Thread-Topic: Test HTML Message #001
Thread-Index: Acuy3U6nqQTa4W4ZQu6ueHPb2nPDPg==
Date: Thu, 13 Jan 2011 04:49:48 +0000
Message-ID: <[email protected]>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
x-originating-ip: [x.x.x.x]
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
X-Spam: [F=0.2000000000; CM=0.500; S=0.200(2010122901)]
X-MAIL-FROM: <[email protected]>
X-SOURCE-IP: [67.136.148.58]
X-AnalysisOut: [v=1.0 c=1 a=WYc53Y7r_f4A:10 a=BLceEmwcHowA:10 a=kj9zAlcOel]
X-AnalysisOut: [0A:10 a=xqWC_Br6kY4A:10 a=ERe/uVw5acJt2lgRGY+7Og==:17 a=10]
X-AnalysisOut: [TGqZ3wK2PL4t1FdnoA:9 a=mfhh-WWaJa5gWHOyyF854Vzt7EAA:4 a=Cj]
X-AnalysisOut: [uIK1q_8ugA:10]

This is a test HTML message.

Did it work?  

There is no MIME declaration and there is no text/html section.

If I send a similar message through Outlook (on the same Exchange server) I get something like this:

Subject: Test From Outlook
Thread-Topic: Test From Outlook
Thread-Index: AcuzMwJCFB8PS6wYQeeV4IeSkT/mxQ==
Date: Thu, 13 Jan 2011 15:03:17 +0000
Message-ID: <[email protected]>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
x-originating-ip: [x.x.x.x]
Content-Type: multipart/alternative;
    boundary="_000_E026BAD23A27DB42B74F0DB569E9986977F237CHALLENGERDenverZ_"
MIME-Version: 1.0
X-Spam: [F=0.2000000000; CM=0.500; S=0.200(2010122901)]
X-MAIL-FROM: <[email protected]>
X-SOURCE-IP: [67.136.148.58]
X-AnalysisOut: [v=1.0 c=1 a=olvCdMnC_oMA:10 a=BLceEmwcHowA:10 a=xqWC_Br6kY]
X-AnalysisOut: [4A:10 a=ERe/uVw5acJt2lgRGY+7Og==:17 a=2iPVs5MlqxP_VJ4oz58A]
X-AnalysisOut: [:9 a=x0_hJeNGqEWpZ67nkUaF-VHO8CMA:4 a=CjuIK1q_8ugA:10 a=yM]
X-AnalysisOut: [hMjlubAAAA:8 a=SSmOFEACAAAA:8 a=bSa8Y9cTgW9e4uXm0lgA:9 a=w]
X-AnalysisOut: [EVqP49dmlVP1afgSrUA:7 a=MTMROL1LpFqQeTH2_C9armvf4mgA:4]

--_000_E026BAD23A27DB42B74F0DB569E9986977F237CHALLENGERDenverZ_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

This is an HTML message.

What do the headers look like?

Tony

--_000_E026BAD23A27DB42B74F0DB569E9986977F237CHALLENGERDenverZ_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
... the correct html code for the message follows here ...

Can anyone explain to me how to get a message like the second one through the code using EWS? Thanks!

Answer

Dana Cartwright picture Dana Cartwright · Feb 17, 2011

I just had a very similar problem. We finally nailed it down to one particular account that was receiving a mangled version of the email instead of the proper multi-part MIME text and HTML. It turns out the problem was this account was setup in Exchange to have "Use Default Settings" for MAPI Rich Text emails (in the General tab), when other accounts were set to "Never".

So the problem was basically that Exchange knew about the contact email being sent to and tried to be "smart" and send it in proprietary format for Outlook, but the client for that address wasn't using Outlook and so they weren't able to view the email correctly. I think it's also possible to change this setting at a global level instead of by user, but I'm not sure where that is.

The reason it works when sent through Outlook is that Outlook 2010 is able to send the email in a way that forces the message to be in HTML format, whereas EWS managed API doesn't seem to be able to do that.