0 votes
by (240 points)

I'm trying to access the Textbody of emails on exchange. But somehow the only thing I recieve is a "null".

Any suggests on this?

            EwsMessageCollection list = await ewsClient.GetMessageListAsync(EwsFolderId.Inbox, EwsItemFields.Envelope, EwsPageView.CreateIndexed(i, Math.Min(PageSize, folderInfo.ItemsTotal - i)));
                            foreach (EwsMessageInfo item in list)
                            {
                                Console.WriteLine($"{item.TextBody}");
                            }
Applies to: Rebex Secure Mail

2 Answers

+1 vote
by (147k points)
selected by
 
Best answer

The EwsItemFields.TextBody field is supported by message-listing methods such as GetMessageList. This seems to be a limitation imposed by the server, and it is a documented behavior - sorry for missing this previously.

To work around this limitation, retrieve a list of IDs first, and use GetMessageInfo to retrieve text body of each message:

EwsMessageCollection list = await ewsClient.GetMessageListAsync(
    EwsFolderId.Inbox,
    EwsItemFields.Id,
    EwsPageView.CreateIndexed(i, Math.Min(PageSize, folderInfo.ItemsTotal - i)));

foreach (EwsMessageInfo item in list)
{
    EwsMessageInfo item2 = await ewsClient.GetMessageInfoAsync(
        item.Id,
        EwsItemFields.Envelope | EwsItemFields.TextBody);
    Console.WriteLine($"{item2.TextBody}");
}

The same limitation applies to AttachmentInfo, Info and Body fields.

by (240 points)
Thanks! This solution works!
by (240 points)
Just a side question on this Topic.

Is it not possible to get the TextBody & Body at the same Time??

For example, here I can access the TextBody:
GetMessageInfoAsync(item.Id, EwsItemFields.Envelope | EwsItemFields.TextBody);

But as soon i try to do this:
GetMessageInfoAsync(item.Id, EwsItemFields.Envelope | EwsItemFields.Full)

I get the Body property but my textBody is null

Any Ideas on that?
by (147k points)
The 'EwsItemFields.Full' enum value is a special case - instead of asking the EWS server to return properties of particular kind(s), it does something like "return all common properties". The problem is that, apparently, the text body is not considered by EWS to be part of those. Thanks for letting us know - we'll fix it for the next release (the client will ask for "all common properties" and for "text body" as well if specified). In the meantime, you can get body the TextBody and Body as long as you don't specify EwsItemFields.Full. So this should work:

GetMessageInfoAsync(item.Id, EwsItemFields.Envelope | EwsItemFields.Body | EwsItemFields.TextBody);
by (240 points)
edited by
Thank you for the answer. Would be a great Update for the next release!
by (147k points)
It turned out a fix for this problem is going to bit more complicated, because EwsItemFields is a flags enum, and EwItemFields.Full actually includes EwItemFields.TextBody as well, making it impossible to determine at runtime whether EwItemFields.TextBody was explicitly specified. So instead, we will add some kind of workaround to the next release that will make it possible to retrieve some extra properties in addtion to EwItemFields.TextBody. We plan to publish the new version next week.
by (240 points)
edited by
Hi Lukas, meanwhile is there any Update on this?
by (147k points)
We already added a workaround and we are currently finalizing the next release. We just got delayed a bit due to the need to pay attention to other unrelated issues as well. So the next release will be most likely published next week. If you would like to try a preview build ahead of the official release, please let us know.
by (147k points)
The new version has been published. To resolve the issue and retrieve the text body as well, call this once before using the relevant Ews methods:
    Rebex.Security.Cryptography.CryptoHelper.SetOption(ews.Settings, "AdditionalFields", EwsItemFields.TextBody);
0 votes
by (147k points)

When the GetMessageList method is called with EwsItemFields.Envelope argument, it's not supposed to return the text body. See Message fields for details.

Try specifying EwsItemFields.TextBody as well:

EwsMessageCollection list = await ewsClient.GetMessageListAsync(
    EwsFolderId.Inbox,
    EwsItemFields.Envelope | EwsItemFields.TextBody,
    EwsPageView.CreateIndexed(i, Math.Min(PageSize, folderInfo.ItemsTotal - i)));
foreach (EwsMessageInfo item in list)
{
    Console.WriteLine($"{item.TextBody}");
}
by (240 points)
Thanks for the quick respond. I have already read the documentary. In between i adjusted the code and specified EwsItemFields.TextBody also as you recommended.

Somehow i'm still encountering the same issue.
item.TextBody is null.
by (147k points)
Does this happen with all messages? Is the HTML body returned in Body property if EwsItemFields.Body is specified instead of TextBody?
by (240 points)
Yes, unfortunately this happens with all messages. (But I am able to access the properties "From, Subject, ReceivedDate," and they contain the correct values.)

In Scenario 1, when specifying EwsItemFields.TextBody, I get null values for both item.TextBody and Item.Body.

In Scenario 2, your recent suggestion was to replace EwsItemFields.TextBody with EwsItemFields.Body. When I do this, I get the following error:

Error: System.ArgumentException: Body field is not supported. (Parameter 'fields')
at Rebex.Net.Ews.GetMessageListAsync(EwsFolderId folderId, EwsItemFields fields, EwsListView listView, Object state)
by (147k points)
Check out my new answer, and sorry for not spotting the problem last week:
https://forum.rebex.net/22441/ews-textbody-gives-null?show=22446#a22446
...