Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 
like image 477
ubergorp Avatar asked Jun 23 '14 08:06

ubergorp


People also ask

What is MailItem?

Items property to obtain an Items collection representing the mail items in a folder, and the Items. Item (index) method, where index is the index number of a mail message or a value used to match the default property of a message, to return a single MailItem object from the specified folder.


2 Answers

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; } 
like image 146
Alex Avatar answered Oct 15 '22 20:10

Alex


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 
like image 33
prgSRR Avatar answered Oct 15 '22 21:10

prgSRR