Function GetFromAddress(ByVal EntryID As String, ByVal ParentStoreID As String, ByRef ReturnFromAddresses As String, Optional ByVal Outlook2000 As Boolean = False) As Boolean ', ByRef ReturnFromAddresses As String) As Boolean
Dim ReturnBool As Boolean = True
'Get the SMTP address of the originator of a message
' You can read the SMTP address of the originator of a message. But if you try to use objAddressEntry.Address you will receive the Exchange address (also known as 'Distinguish Name' if the originator is an Exchange mailbox) and not the SMTP address. Here is how to read the SMTP address:
' MAPI property tag for SMTP address
Dim objSession As New Object, objFolder As New Object, objMessages As New Object, objMessage As New Object
Dim objField As New Object, objCDOMessage As New Object, objAddEntry As New Object
Const CdoPR_EMAIL As Integer = &H39FE001E
Const PR_EMS_AB_PROXY_ADDRESSES As Integer = &H800F101E
Try
Dim strAddressEntryID As String = ""
'set the ReturnFromAddresses to blank
ReturnFromAddresses = ""
strErrorString = "Start MAPI Session"
objSession = CreateObject("MAPI.Session")
strErrorString = "MAPI Session Logon"
objSession.Logon("", "", False, False, 0)
If Outlook2000 Then
'using outlook 2000
' Get first message from inbox
strErrorString = "2000: Get Inbox"
objFolder = objSession.Inbox
strErrorString = "2000: Get Folder Messages"
objMessages = objFolder.Messages
strErrorString = "2000: Get Message(EntryID,StoreID)"
objMessage = objMessages.GetMessage(EntryID, ParentStoreID)
' Get address
strErrorString = "2000: Get Address Entry"
objAddEntry = objMessage.Sender
strErrorString = "2000: Get Address From AddEntry"
ReturnFromAddresses = objAddEntry.Address
' Check if it is an Exchange object
strErrorString = "2000: Check for /O="
If Microsoft.VisualBasic.Left(ReturnFromAddresses, 3).ToUpper = "/O=" Then
' Get the SMTP address
strErrorString = "2000: Get CdoPR_Email"
strAddressEntryID = objAddEntry.ID
ReturnFromAddresses = objSession.GetAddressEntry(strAddressEntryID).Fields(CdoPR_EMAIL).Value()
End If
Else
'using outlook 2002 +
strErrorString = "2002+: Get CDO Message"
objCDOMessage = objSession.GetMessage(EntryID, ParentStoreID)
strErrorString = "2002+: Get CDO Sender"
objAddEntry = objCDOMessage.Sender
strErrorString = "2002+: Get PR_EMS_AB_PROXY_ADDRESSES"
objField = objAddEntry.Fields(PR_EMS_AB_PROXY_ADDRESSES)
strErrorString = "2002+: Loop Through Addresses"
For Each strEMailAddress As String In objField.Value
If Microsoft.VisualBasic.Left(strEMailAddress, 5) = "SMTP:" Then
ReturnFromAddresses = Microsoft.VisualBasic.Mid(strEMailAddress, 6)
Exit For
End If
Next
End If
' Display the SMTP address of current user
MessageBox.Show("SMTP address of current user: " & ReturnFromAddresses, UtilityName, MessageBoxButtons.OK, MessageBoxIcon.Information)
'Note that you must use exactly the way described above. Otherwise you will fail to get the SMTP address.
Catch exp As System.UnauthorizedAccessException
MessageBox.Show("If you say NO to the security boxes then email will NOT be gathered", "Email Inbox", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
ReturnBool = False
Exit Try
Catch exp As System.Exception
MessageBox.Show("GetAddress error: " & strErrorstring & vbCrLf & exp.Message, UtilityName, MessageBoxButtons.OK)
ReturnBool = False
End Try
'cleanup
objSession = Nothing : objFolder = Nothing : objMessages = Nothing : objMessage = Nothing : objField = Nothing : objCDOMessage = Nothing : objAddEntry = Nothing
ReturnBool = Not IsNullOrEmpty(ReturnFromAddresses)
End Function