Carter
Carter

Reputation: 2870

How do I determine the sender of an email via Exchange Web Services in C#?

I'm currently pulling emails from an exchange inbox like so...

var exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
{
    Credentials = new NetworkCredential("user", "password", "domain")
};

exchangeService.AutodiscoverUrl("[email protected]");

var emails = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(5));

foreach (var email in emails)
{
    //var senderEmail = email.???
}

The email object doesn't seem to have any property for getting the sender's email address. How do I get that?

Upvotes: 4

Views: 2453

Answers (1)

ChrisW
ChrisW

Reputation: 9399

Here's some quick source I pulled from a working project example.

Basically, you can get minor details just by casting your result to an EmailMessage. However if you want to get richer details about the sender (display name, etc.) then you have to make a specific, additional bind (Web service request) against the message.

findResults = exchangeService.FindItems(folder.Id, messageFilter, view);
            foreach (Item item in findResults)
            {
                if (item is EmailMessage)
                {
                    EmailMessage message;
                    if (!toFromDetails)
                        message = (EmailMessage)item;
                    else
                        message = EmailMessage.Bind(exchangeService, item.Id);

As you can see in this code, I have an option to perform the additional bind, because it can take awhile, and I'm often dealing with thousands of results from hundreds of mailboxes. Sometimes the additional time may not be worth it to a particular customer.

Upvotes: 7

Related Questions