We help IT Professionals succeed at work.

Retrieve SMTP address from Exchange Addresses

fbict
fbict asked
on
Hi All,

Within Outlook 2003 I use the CDO1.21 library to relay mail to our helpdesk server.

It all works fine, but I like to know the SMTP-address from the sender..

If the sender has an external mail adres, it works fine....
But, if the sender is one of our network-users (exchange clients), then I am not able to retrieve the SMTP address...

Microsoft worked out the following solution, using CDO...

http://support.microsoft.com/?scid=http://support.microsoft.com%2Fservicedesks%2Fbin%2Fkbsearch.asp%3Farticle%3D324530

But that does not work correctly with me... I get the user display name, that is ok, but the SMTP address says:

/o=mycompany/ou=central administrative group/cn=recipients/cn=user001

I found two methods, but in both cases the program fails... straddress is then still empty :(

1. code

       If Not InStr(strAddress, "@") Then
         On Error Resume Next
         strAddress = objItem.Sender.Fields(g_PR_SMTP_ADDRESS_W).Value
      End If
       

2. code

Set objReply = objItem.Reply
    Set objRecips = objReply.Recipients
   
      For Each objRecip In objRecips
      MsgBox ("Objrecip: " & objRecip.Address)
      strAddress = objRecip.Address

i have set the   Const g_PR_SMTP_ADDRESS_W = &H39FE001F and other vars en libs..

I also tried the examples from Microsoft, but the program refuses to get into the IF/THEN loop:
============================  
Dim objSession As MAPI.Session
   Dim objInboxMsgs As MAPI.Messages
   Dim objMsg As MAPI.Message
   Dim strAddress As String

   Const g_PR_SMTP_ADDRESS_W = &H39FE001F
   
   Set objSession = CreateObject("MAPI.Session")
   objSession.Logon NewSession:=False
   Set objInboxMsgs = objSession.Inbox.Messages
 For Each objMsg In objInboxMsgs
      Debug.Print objMsg.Subject
      strAddress = objMsg.Sender.Address
      If Not InStr(strAddress, "@") Then
         On Error Resume Next
         strAddress = objMsg.Sender.Fields(g_PR_SMTP_ADDRESS_W).value
      End If
      Debug.Print strAddress
      Debug.Print
   Next
============================


Can someone give me an hint/example to retrieve the smtp-address from the Current Mailitem? So I only want to know the smtp-adress from the selected email.

As I said external address works fine...

Hope someone can help me... (of course you can :))

Kind Regards,

Roger Wilikook
The Netherlands.
Comment
Watch Question

Hi Roger,

I had a similar issue when I accessed my companies GAL via VBA, I used
&H39FE001E rather than &H39FE001F
to download the valid address, see http://www.vbaexpress.com/kb/getarticle.php?kb_id=222

I can't test this now as I'm not at work and I'm a little rusty on CDO but I hope this helps.

Regards

Dave

Commented:
thats because the mails within the organization dont get relayed using SMTP, since its controlled by Microsoft Exchange Server. You will need to default an SMTP address incase if its not found in the email. What you are recieving is the relay address.
I'd suggest use the AddressEntry property rather than Address property of Recipients property of Mail item.

MessageBox objMailItem.Recipients.AddressEntry.Address

See this page: http://msdn.microsoft.com/library/en-us/vbaol10/html/olproAddressEntry.asp

Author

Commented:
pratap r tells that the /o=mycompany/ou=central administrative group/cn=recipients/cn=user001 is the relay address.... ok I understand, but i need to know the SMTP.

All the accounts have a primary smtp address, but how to retrieve?

I forgot to mention that I can get the 'display name' from the sent email, like "Smith, John"...

but the question stays the same: but how to retrieve the smtp address?
AddressEntry.Address returns you the RESOLVED address.

Author

Commented:
Hi RanjeetRain,

it works for external mail addresses, but for Exchange items it does not work.

So a mail from an external user the AddressEntry look like:

External:
 Recipient full address = SMTP:info@externaluser.nl
 AddressEntry type = SMTP
 AddressEntry address = info@externaluser.nl

But a mail from an internal user the AddressEntry look like:

Exchange:
 Recipient full address = EX:/O=DOMAIN/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=MyUser
 AddressEntry type = EX
 AddressEntry address = /o=DOMAIN/ou=first administrative group/cn=Recipients/cn=MyUser"

The code I use is:

==============================================
   Set objSession = CreateObject("MAPI.Session")
    objSession.Logon

Set objFolder = objSession.Inbox
Set objMessages = objFolder.Messages
Set objOneMsg = objMessages.GetLast
Set objRecipColl = objOneMsg.Recipients

If 0 = objRecipColl.Count Then
    MsgBox "No recipients in the list"
End If

iRecipCollIndex = 1
Set objOneRecip = objRecipColl.Item(iRecipCollIndex)
Set objAddrEntry = objOneRecip.AddressEntry
  strmsg = "Recipient full address = " & objOneRecip.Address & vbCrLf
  strmsg = strmsg & " AddressEntry type = " & objAddrEntry.Type & vbCrLf
  strmsg = strmsg & " AddressEntry address = " & objAddrEntry.Address
MsgBox strmsg
==============================================

So, how to get the SMTP address from an Exchange (sent) message (type=EX)?
Commented:
hmm.. unfortunately exchange server does not notify the client about the smtp server it uses finally to relay the mail(only applicable for mails sent to a person outside the organization). If the mail is sent within the company (same exchange server) the mail does not travel through SMTP. So there is no SMTP server involved. This is the same case for a mail that you send from your system. Your client machine (running outlook) does not need to know about SMTP, so the information is not stored at all. FYI, In most of the cases the exchange server also serves SMTP requests for within the organization.