如何从 Exchange Web 服务 (2010 SP1) 发送 HTML(多部分/替代)
当我尝试通过 EWS 发送带有内嵌图像(附件)的 HTML 消息时,发现了这个问题。然而,我已经退回到最简单的形式,发现我什至无法发送 HTML 消息。
我使用的是 C#、.NET 4、EWS API 1.1、Exchange Server 2010 SP1。
这是我的代码的核心:
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();
使用该代码,我收到一条包含以下内容的消息:
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?
没有 MIME 声明,也没有 text/html 部分。
如果我通过 Outlook(在同一台 Exchange 服务器上)发送类似的消息,我会收到类似以下内容:
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 ...
任何人都可以向我解释如何使用 EWS 通过代码获取类似于第二条消息的消息吗?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了一个非常相似的问题。我们最终将其确定为一个特定帐户,该帐户收到的是电子邮件的损坏版本,而不是正确的多部分 MIME 文本和 HTML。事实证明,问题在于该帐户在 Exchange 中设置为对 MAPI 富文本电子邮件(在“常规”选项卡中)使用“使用默认设置”,而其他帐户设置为“从不”。
因此,问题基本上是 Exchange 知道要发送到的联系人电子邮件,并尝试“智能”并以 Outlook 专有格式发送,但该地址的客户端没有使用 Outlook,因此他们无法正确查看电子邮件。我认为也可以在全局级别而不是用户更改此设置,但我不确定它在哪里。
通过 Outlook 发送时它起作用的原因是 Outlook 2010 能够以强制邮件采用 HTML 格式的方式发送电子邮件,而 EWS 托管 API 似乎无法做到这一点。
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.
我刚刚发现这个页面 解释发生了什么以及如何更改设置。
这是他们的结论:
I just found this page which explains what is happening and how to change the setting.
This is their conclusion: