In EWS, how to get message's both plain-text and HTML bodies as strings keeping the original character encoding

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-07 07:41:32

问题


I'm working with Exchange 2010 (not Exchange 2013 which lets the caller request both plain-text and HTML bodies directly).

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;

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).

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?


回答1:


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.



来源:https://stackoverflow.com/questions/40810298/in-ews-how-to-get-messages-both-plain-text-and-html-bodies-as-strings-keeping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!