3

I am trying to get the real email address of the sender of an email using Exchange web services, however the mailitem.Sender.Address contains something like -

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (...........)/CN=RECIPIENTS/CN=...........-.....

How can I get to the real email address of the sender of this email?

My code:

Dim sf As SearchFilter = New SearchFilter.SearchFilterCollection(LogicalOperator.And, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False))
Dim findResults As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, sf, New ItemView(128))
Dim items As ServiceResponseCollection(Of GetItemResponse) = service.BindToItems(findResults.Select(Function(item) item.Id), New PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients))

For Each itItem As Microsoft.Exchange.WebServices.Data.Item In findResults.Items
      If TypeOf itItem Is EmailMessage Then
            Dim mailItem As EmailMessage = DirectCast(itItem, EmailMessage)

And then I want to refer to mailItem.Sender.Address, which contains the above mentioned string instead of abc@whatever.com

Some example code, preferably in VB.NET, would be appreciated as I have a hard time figuring out how these Exchange web services work.

Community
  • 1
  • 1
George
  • 1,111
  • 3
  • 20
  • 38

2 Answers2

5

I'd like to answer my own question:

Dim instances As NameResolutionCollection
instances = service.ResolveName(mailItem.Sender.Address)
If instances.Count > 0 Then
    ResolveName = instances(0).Mailbox.Address
Else
    ResolveName = ""
End If

... where "service" is an ExchangeService object and mailItem.Sender.Address contains an X500 address (I think that's what it's called). mailItem.Sender.Address will contain an X500 type address if the sender is intern to your organisation as Jan Doggen pointed out.


I might recommend changing to the following:

If instances.Count > 0 Then
    ResolveName = instances(0).Mailbox.Address
Else
    ResolveName = i.Sender.Address
End If

By doing this the ResolveName will keep the original sender Email Address if the email is from an external source.

Community
  • 1
  • 1
George
  • 1,111
  • 3
  • 20
  • 38
1

Quoting from How to: Get the SMTP Address of the Sender of a Mail Item:

"To determine the SMTP address for a received mail item, use the SenderEmailAddress property of the MailItem object. However, if the sender is internal to your organization, SenderEmailAddress does not return an SMTP address, and you must use the PropertyAccessor object to return the sender’s SMTP address."

The page gives a C# example, that you should be able to convert to VB.Net:

private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
    string PR_SMTP_ADDRESS =
        @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
    if (mail == null)
    {
        throw new ArgumentNullException();
    }
    if (mail.SenderEmailType == "EX")
    {
        Outlook.AddressEntry sender =
            mail.Sender;
        if (sender != null)
        {
            //Now we have an AddressEntry representing the Sender
            if (sender.AddressEntryUserType ==
                Outlook.OlAddressEntryUserType.
                olExchangeUserAddressEntry
                || sender.AddressEntryUserType ==
                Outlook.OlAddressEntryUserType.
                olExchangeRemoteUserAddressEntry)
            {
                //Use the ExchangeUser object PrimarySMTPAddress
                Outlook.ExchangeUser exchUser =
                    sender.GetExchangeUser();
                if (exchUser != null)
                {
                    return exchUser.PrimarySmtpAddress;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return sender.PropertyAccessor.GetProperty(
                    PR_SMTP_ADDRESS) as string;
            }
        }
        else
        {
            return null;
        }
    }
    else
    {
        return mail.SenderEmailAddress;
    }
}
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
  • Thanks for your reply, however this doesn't seem to apply to Exchange Web Services, my mail item doesn't have a property SenderEmailType. – George Mar 14 '14 at 08:24