在EWS中,如何获取消息的纯文本和HTML正文作为字符串,以保持原始字符编码 [英] In EWS, how to get message's both plain-text and HTML bodies as strings keeping the original character encoding
问题描述
我正在使用Exchange 2010(不是Exchange 2013,它允许调用者直接请求纯文本和HTML正文).
I'm working with Exchange 2010 (not Exchange 2013 which lets the caller request both plain-text and HTML bodies directly).
要获取HTML正文,我使用的是类似的内容:
To get HTML body, I'm using something like:
ExtendedPropertyDefinition PR_BODY_HTML = new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary);
ExtendedPropertyDefinition PR_INTERNET_CPID = new ExtendedPropertyDefinition(0x3FDE, MapiPropertyType.Long);
PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);
properties.RequestedBodyType = BodyType.Text;
properties.Add(EmailMessageSchema.Body);
properties.Add(PR_BODY_HTML);
properties.Add(PR_INTERNET_CPID);
...
byte[] htmlBodyBytes;
string htmlBody;
int iCP;
if (item.TryGetProperty<int>(PR_INTERNET_CPID, out iCP))
{
// The code never enters here
}
if (item.TryGetProperty<byte[]>(PR_BODY_HTML, out htmlBodyBytes))
{
htmlBody = Encoding.GetEncoding(65001).GetString(htmlBodyBytes);
}
string textBody = item.Body.Text;
对于纯文本主体,我得到了正确的字符串表示形式.但是HTML正文只给我字节,我也不知道要传递给GetString的代码页.目前,UTF-8的代码页已进行硬编码,但这不适用于生产环境.我需要找到HTML部分的代码页,或者找到从消息中提取它的另一种方法.当然,我可以对EWS设置RequestedBodyType = BodyType.HTML进行单独查询,但最好不要进行其他查询.我以为PR_INTERNET_CPID MAPI属性(0x3FDE)可以满足我的需要,但是从来没有填充过(我再次检查它是否存在于邮件服务器上,但我无法通过EWS获得它).
For plain-text body, I get the correct string representation. But HTML body gives me just bytes and I don't know the codepage to pass to GetString. Currently, UTF-8's codepage is hardcoded but this won't work for production. I need to either find out the codepage of the HTML part or find another method of extracting it from the message. Of course, I could make a separate query to EWS setting RequestedBodyType = BodyType.HTML but I would better not make an additional query. I thought PR_INTERNET_CPID MAPI property (0x3FDE) will fit my needs but it's never populated (I double-checked that it exists on the mail server but I can't get it via EWS).
因此,我需要说服托管EWS库以字符串形式返回HTML和纯文本,或者让我获得PR_INTERNET_CPID值.我该怎么办?
So I need to either convince Managed EWS library to return both HTML and plain-text as strings or get me PR_INTERNET_CPID value. What can I do for that?
推荐答案
好,事实证明PidTagInternetCodepage(PR_INTERNET_CPID)的类型为MapiPropertyType.Integer,而不是MapiPropertyType.Long(尽管MSDN表示PT_LONG).调整后,我可以很好地得到有关的值.
OK, it turns out that PidTagInternetCodepage (PR_INTERNET_CPID) has type MapiPropertyType.Integer, not MapiPropertyType.Long (although MSDN says PT_LONG). After the adjustment, I can get the value in question just fine.
这篇关于在EWS中,如何获取消息的纯文本和HTML正文作为字符串,以保持原始字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!