How can I get the sender email address using Outlook.MailItem in VB.NET?

ubergorp picture ubergorp · Jun 23, 2014 · Viewed 37.7k times · Source

I have tried using mailItem.SenderEmailAddress and mailItem.Sender.Address but they both return a string that looks like this:

/O=DOMAINNAME/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHI43SPCLT)/CN=RECIPIENTS/CN=JOE BLOGGS8C3

Where in reality I want [email protected] to be retrurned.

Anyone have any ideas?

Thank you very much.

Edit: I have done some digging; it works perfectly for email addresses of the 'SenderEmailType' SMTP, it just doesn't work for Exchange email addresses.

Edit 2: I have tried the code specified here, but I assume it is outdated because it throws a "Cannot create Active-X component" error.

EDIT 3: For anyone who ever has the same problem as me, I found the answer (in C#, converted to VB.NET, still works though):

Private Function GetSenderSMTPAddress(mail As Outlook.MailItem) As String
    Dim PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
    If mail Is Nothing Then
        Throw New ArgumentNullException()
    End If
    If mail.SenderEmailType = "EX" Then
        Dim sender As Outlook.AddressEntry = mail.Sender
        If sender IsNot Nothing Then
            'Now we have an AddressEntry representing the Sender
            If sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry OrElse sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then
                'Use the ExchangeUser object PrimarySMTPAddress
                Dim exchUser As Outlook.ExchangeUser = sender.GetExchangeUser()
                If exchUser IsNot Nothing Then
                    Return exchUser.PrimarySmtpAddress
                Else
                    Return Nothing
                End If
            Else
                Return TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String)
            End If
        Else
            Return Nothing
        End If
    Else
        Return mail.SenderEmailAddress
    End If
End Function

Answer

Alex picture Alex · Jul 2, 2014

I see you have answered your own question. I will post my C# function here incase anybody needs it or if you would like to use it as more help. My C# function for doing what you do looks like this:

 private string getSenderEmailAddress(Outlook.MailItem mail)
{
 Outlook.AddressEntry sender = mail.Sender;
 string SenderEmailAddress = "";

  if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
    {
        Outlook.ExchangeUser exchUser = sender.GetExchangeUser();
        if (exchUser != null)
        {
            SenderEmailAddress = exchUser.PrimarySmtpAddress;
        }
    }
    else
    {
        SenderEmailAddress = mail.SenderEmailAddress;
    }

    return SenderEmailAddress;
}