Vishal Patel
Vishal Patel

Reputation: 41

How to read latest email conversation HTML body from specific email using Microsoft Exchange webservice

I am using EWS to read email body part alone from email of inbox.

I need to extract only replied email body instead of whole email body.

e.g.

************
A This is good tenant.

Regards,
Test

From:[email protected]
To: ----
----------
----------

Hi User, Data has been populated. Please reply with A or R with comments.

Regard
Admin.

************

So when I read email body of above email I get the whole body mentioned above. But what I need is only:

************
A This is good tenant.

Regards,
Test
************

which is having latest replied email body only.

Upvotes: 0

Views: 3938

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

This approach with UniqueBody works for me:

// ensure that username, password, domain and smtpAddress are set
var service = new ExchangeService {
    PreAuthenticate = true,
    Credentials = new WebCredentials(username, password, domain),
};
service.AutodiscoverUrl(smtpAddress, redirect => true);

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, smtpAddress);
var inbox = Folder.Bind(service, new FolderId(WellKnownFolderName.Inbox));
var fir = inbox.FindItems(new ItemView(10));
foreach (var ir in fir) {
    var msg = EmailMessage.Bind(service, ir.Id, new PropertySet(EmailMessageSchema.UniqueBody));
    Console.WriteLine(msg.UniqueBody.Text);
}

For any follow-up message in the results, the msg.UniqueBody.Text property contains only the parts that are new in that message.

Note that there might be better ways to do this, but this works in my quick test (against Exchange Online).

Upvotes: 2

Related Questions