如何从 Exchange Web 服务 (2010 SP1) 发送 HTML(多部分/替代)

发布于 2024-10-12 00:42:36 字数 4132 浏览 4 评论 0原文

当我尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-10-19 00:42:36

我刚刚遇到了一个非常相似的问题。我们最终将其确定为一个特定帐户,该帐户收到的是电子邮件的损坏版本,而不是正确的多部分 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.

聆听风音 2024-10-19 00:42:36

我刚刚发现这个页面 解释发生了什么以及如何更改设置。

这是他们的结论:

我们检查的最后一件事揭示了运行以下交换 powershell“get-remotedomain | ”的问题所在。佛罗里达州”。然后我们发现内容类型设置为 MimeText,它“将所有消息转换为使用文本格式的 MIME 消息”(即纯文本)。然后,我们将内容类型更改为 mimehtmltext(将消息转换为使用 HTML 格式的 MIME 消息,除非原始消息是文本消息。如果原始消息是文本消息,则出站消息是使用文本格式的 MIME 消息)运行以下命令“get-remotedomain |设置远程域-ContentType MimeHtmlText”。

I just found this page which explains what is happening and how to change the setting.

This is their conclusion:

The final thing we checked which revealed what the issue was to run the following exchange powershell “get-remotedomain | fl” . We then spotted that the content type was set to MimeText which “converts all messages to MIME messages that use text formatting” (i.e plain text). We then changed the content type to mimehtmltext (Converts messages to MIME messages that use HTML formatting, unless the original message is a text message. If the original message is a text message, the outbound message is a MIME message that uses text formatting) by running the following command “get-remotedomain | set-remotedomain -ContentType MimeHtmlText”.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文