如何使用C#中的EWS从Exchange获取HTML和TEXT中的电子邮件正文?

时间:2014-01-07 22:01:02

标签: c# exchangewebservices

我有一个应用程序,使用EWS从Exchange中读取电子邮件。我的问题是,获取电子邮件的HTML版本是一个电话,并获得电子邮件的TEXT版本是另一个电话。

如果没有第三方控件可以在一次调用中获取这两种格式吗?

拥有一些示例代码会很棒。

1 个答案:

答案 0 :(得分:6)

看看Exchange Server开发论坛上的这个主题,我想它会回答你的问题。 http://social.technet.microsoft.com/Forums/exchange/en-US/3c95b323-1ba2-4bc5-80bd-f5626707db6a/i-need-the-htmltext-and-the-plaintext-of-the-body-of-an-itemtype?forum=exchangesvrdevelopment


更新

我玩这个,所以我可以提供一个代码示例,结果证明你不必使用扩展属性。默认情况下,EWS在EmailMessageSchema.Body中返回HTML格式的主体 - 因此,如果您创建一个包含ItemSchema.TextBody和EmailMessageSchema.Body的属性集,则可以在一个Bind调用中获取这两种类型。

public static void GetEmail(ExchangeService service, ItemId ItemId)
{
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.TextBody, EmailMessageSchema.Body);
    EmailMessage message = EmailMessage.Bind(service, ItemId, propSet);
}

这会产生以下XML请求:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
  <t:RequestServerVersion Version="Exchange2013" />
</soap:Header>
<soap:Body>
  <m:GetItem>
    <m:ItemShape>
      <t:BaseShape>IdOnly</t:BaseShape>
      <t:AdditionalProperties>
        <t:FieldURI FieldURI="item:TextBody" />
        <t:FieldURI FieldURI="item:Body" />
      </t:AdditionalProperties>
    </m:ItemShape>
    <m:ItemIds>
      <t:ItemId Id="AAMkADE4..." />
    </m:ItemIds>
  </m:GetItem>
</soap:Body>

以下回复:

<?xml version="1.0" encoding="utf-8"?>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="878" MinorBuildNumber="11" Version="V2_10" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    <m:ResponseMessages>
      <m:GetItemResponseMessage ResponseClass="Success">
        <m:ResponseCode>NoError</m:ResponseCode>
        <m:Items>
          <t:Message>
            <t:ItemId Id="AAMkADE4..." ChangeKey="CQAAABYAAAApjGm7TnMWQ5TzjbhziLL0AAGTja3C" />
            <t:Body BodyType="HTML" IsTruncated="false">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD  
                HTML 4.0 Transitional//EN"&gt;
                &lt;html&gt;
                &lt;head&gt;
                &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
               (Removed the rest of my HTML body)
            </t:Body>
            <t:TextBody BodyType="Text" IsTruncated="false">
             (Removed my text body)
            </t:TextBody>
          </t:Message>
        </m:Items>
      </m:GetItemResponseMessage>
    </m:ResponseMessages>
  </m:GetItemResponse>
</s:Body>

希望有所帮助! 米米