Solved

Getting the SMTP email address of an email's sender in Outlook

Posted on 2014-10-21
4
158 Views
Last Modified: 2014-10-23
Hello Experts

I am having problems retrieving the sender of an email in Outlook 2010 - in some cases.

at the moment I am using the following code - courtesy of BlueDevilFan  (see
this link) to get the address back:

Function GetSMTPAddress(olkMsg As Outlook.MailItem) As String
    Dim olkSnd As Outlook.AddressEntry, olkExu As Outlook.ExchangeUser
    Set olkSnd = olkMsg.Sender
    If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
       Set olkExu = olkSnd.GetExchangeUser
        GetSMTPAddress = olkExu.PrimarySmtpAddress

    Else
        GetSMTPAddress = olkMsg.SenderEmailAddress
    End If
    Set olkSnd = Nothing
    Set olkExu = Nothing
End Function

However, there are some cases (just 2 cases out of 75 in my initial testing) where this just returns an empty string.

On stepping through the code, the cases are both ones where the AddressEntryUser Type equals
OLExchangeUserAddressEntry (i.e. the lines in bold get executed).

Can anyone out there explain why this happens and how to get the proper SMTP address back, as opposed to the X400 Exchange address? Or is it just that the sending Exchange server has been badly configured?

Thanks in advance.

Hopeful Kiwi
0
Comment
Question by:kiwi_731
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
sinfocomar earned 500 total points
ID: 40395298
Blue..'s script is somewhat lacking. Please try this one:

http://www.gregthatcher.com/Scripts/VBA/Outlook/GetSmtpAddress.aspx
0
 

Author Comment

by:kiwi_731
ID: 40399603
Hi sinfocomar

The script you linked to works beautifully. The intractable cases now produce the correct output via the GetSmtpAddress = exchangeUser.PrimarySmtpAddress line of code.

I admit to being nonplussed as to how the routine works; however, it works. Delving into the depths of MAPI etc is a task for another day. (Why did they have to make it so complicated?)

Many thanks

Hopeful Kiwi
0
 

Author Closing Comment

by:kiwi_731
ID: 40399618
Good solution, a bit light on details, but not to worry.

Hopeful Kiwi
0
 
LVL 11

Expert Comment

by:sinfocomar
ID: 40399747
Sorry for the lightness :) all I knew was the script worked...

Microsoft seems to not like revealing the inner workings of Outlook (wonder why...). Pity as it was supposed to be an open PIM for third parties to add their stuff... We should be neck deep in code and plugins by now that do these things. Also not every MAPI property is exposed to VBA but only MFC if you want to read some look here: http://www.slipstick.com/developer/read-mapi-properties-exposed-outlooks-object-model/

Thx for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video discusses moving either the default database or any database to a new volume.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now