'How to get the smtp email address located on the left of the "on behalf of" address

I would like to get the email address from the true contact that is sending the mailItem. I don't want the on behalf email address.

Is there a way to do that?

Outlook mailItem example

enter image description here

This is my code:

Function GetSenderSMTPAddress(ByRef olkMailItem As Outlook.MailItem) As String
    Dim olkSender As AddressEntry
    Set olkSender = olkMailItem.sender

    GetSenderSMTPAddress = GetSMTPAddress(olkSender)
End Function

Function GetSMTPAddress(ByRef olkAddress As AddressEntry) As String
    Dim smtpAddress As String: smtpAddress = olkAddress.Address
    Dim olkExUser As ExchangeUser
    
    If olkAddress.AddressEntryUserType = olExchangeUserAddressEntry Or _
    olkAddress.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then
        Set olkExUser = olkAddress.GetExchangeUser
    End If
    
    If Not IsNull(olkExUser) Then
        smtpAddress = olkExUser.PrimarySmtpAddress
    End If
    
    GetSMTPAddress = smtpAddress
End Function


Solution 1:[1]

I have answered my own question creating a recipient with the display name, getting the addressEntry from that recipient and using the function that I have already developed to get the smtp address for the given addressEntry.

See the next code:

Function GetSenderSMTPAddress(ByRef olkNS As outlook.Namespace, ByRef olkMailItem As outlook.MailItem) As String
    Dim strSenderName As String
    strSenderName = olkMailItem.SenderName
    
    Dim olkRecipient As Recipient
    Set olkRecipient = olkNS.CreateRecipient(strSenderName)
    
    Dim olkAddressEntry As AddressEntry
    Set olkAddressEntry = olkRecipient.AddressEntry
    
    GetSenderSMTPAddress = GetSMTPAddress(olkAddressEntry)
End Function

Function GetSMTPAddress(ByRef olkAddress As AddressEntry) As String
    Dim smtpAddress As String: smtpAddress = olkAddress.Address
    Dim olkExUser As ExchangeUser
    
    If olkAddress.AddressEntryUserType = olExchangeUserAddressEntry Or _
    olkAddress.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then
        Set olkExUser = olkAddress.GetExchangeUser
    End If
    
    If Not IsNull(olkExUser) Then
        smtpAddress = olkExUser.PrimarySmtpAddress
    End If
    
    GetSMTPAddress = smtpAddress
End Function

Solution 2:[2]

The SMTP address will most likely be set on the message as a MAPI property that you can access using MailItem.PropertyAccessor.GetProperty - take a look at PR_SENT_REPRESENTING_SMTP_ADDRESS_W (DASL name http://schemas.microsoft.com/mapi/proptag/0x5D02001F) and PidTagSenderSmtpAddress_W (DASL name http://schemas.microsoft.com/mapi/proptag/0x5D01001F) instead of using GetExchangeUser() - take a look at the message with OutlookSpy (I am its author) - click IMessage button.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2