I have tried using mailItem.SenderEmailAddress
and mailItem.Sender.Address
but they both return a string that looks like this:
/O=DO
If someone's still looking for a solution to this problem, here is a simplified and true-blue VBA version of the code to handle this requirement.
Public Sub GetCurrentItem()
On Error Resume Next
Set ObjSelectedItem = Outlook.ActiveExplorer.Selection.Item(1)
If TypeName(ObjSelectedItem) = "MailItem" Then
If ObjSelectedItem.SenderEmailType = "EX" Then
MsgBox (ObjSelectedItem.Sender.GetExchangeUser.PrimarySmtpAddress)
Else
MsgBox (ObjSelectedItem.SenderEmailAddress)
End If
Else
MsgBox ("No items selected (OR) Selected item not a MailItem.")
End If
Set ObjSelectedItem = Nothing
End Sub
Created a VBA function if you wanted to use it for simplicity. A sample call would be
Left(GetEmailAddress(mai) & Space(50), 50)
where mai
is expected to be a MailItem
object. Used and tested successfully in Microsoft Outlook 2010
Public Function GetEmailAddress(mai As Object) As String
On Error Resume Next
Set ObjSelectedItem = mai
If TypeName(ObjSelectedItem) = "MailItem" Then
If ObjSelectedItem.SenderEmailType = "EX" Then
GetEmailAddress = ObjSelectedItem.Sender.GetExchangeUser.PrimarySmtpAddress
Else
GetEmailAddress = ObjSelectedItem.SenderEmailAddress
End If
Else
GetEmailAddress = "Not a MailItem"
End If
Set ObjSelectedItem = Nothing
End Function
To me a much simpler answer is as follows
Where to get an external address you may have used SenderEmailAddress, then for an internal (i.e. from exchange) address use Sender.GetExchangeUser.PrimartySmtpAdress instead
If you want it to work for both internal and external addresses then put in a test to see whether the address was internal or external first. Example code snippet below
If itm.SenderEmailType = "SMTP" Then
mailfrom = itm.SenderEmailAddress
Else
If itm.SenderEmailType = "EX" Then
mailfrom = itm.Sender.GetExchangeUser.PrimarySmtpAddress
End If
End If
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;
}
VBA Solution as well (Just translated the VB.net)
Private Function GetSenderSMTPAddress(mail As Outlook.MailItem) As String
If mail Is Nothing Then
GetSenderSMTPAddress = vbNullString
Exit Function
End If
If mail.SenderEmailType = "EX" Then
Dim sender As Outlook.AddressEntry
Set sender = mail.sender
If Not sender Is Nothing Then
'Now we have an AddressEntry representing the Sender
If sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry Or sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then
'Use the ExchangeUser object PrimarySMTPAddress
Dim exchUser As Outlook.ExchangeUser
Set exchUser = sender.GetExchangeUser()
If Not exchUser Is Nothing Then
GetSenderSMTPAddress = exchUser.PrimarySmtpAddress
Else
GetSenderSMTPAddress = vbNullString
End If
Else
GetSenderSMTPAddress = sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
End If
Else
GetSenderSMTPAddress = vbNullString
End If
Else
GetSenderSMTPAddress = mail.SenderEmailAddress
End If
End Function