Read sender's email address from MS Outlook mail

后端 未结 2 1149
一整个雨季
一整个雨季 2020-12-19 12:53

I am using below code to read incoming mails from MS Outlook 2010 -

public static void outLookApp_NewMailEx(string EntryIDCollection)
{                
             


        
相关标签:
2条回答
  • 2020-12-19 13:19

    The address returned now is an (X.400) Exchange address. Please take a look at this MSDN article on how to retrieve the corresponding SMTP address.

    0 讨论(0)
  • 2020-12-19 13:25

    I am using this VBA routine to retrieve the SMTP address of a mailItem object:

    (should be easily portable to C#)

    Private Function getSmtpMailAddress(sMail As Outlook.mailItem) As String
        Dim strAddress As String
        Dim strEntryId As String
        Dim objRecipient As Outlook.Recipient
        Dim objSession As Outlook.NameSpace
        Dim objAddressentry As Outlook.AddressEntry
        Dim objExchangeUser As Outlook.ExchangeUser
        Dim objReply As Outlook.mailItem
    
        On Error GoTo ErrHandler
    
        If sMail.SenderEmailType = "SMTP" Then
            strAddress = sMail.SenderEmailAddress
        Else
            Set objReply = sMail.reply()
            Set objRecipient = objReply.recipients.item(1)
    
            strEntryId = objRecipient.EntryID
    
            objReply.Close OlInspectorClose.olDiscard
    
            Set objSession = getMapiSession
    
            strEntryId = objRecipient.EntryID
    
            Set objAddressentry = objSession.GetAddressEntryFromID(strEntryId)
            Set objExchangeUser = objAddressentry.GetExchangeUser()
    
            strAddress = objExchangeUser.PrimarySmtpAddress()
        End If
    
        getSmtpMailAddress = strAddress
    
        Exit Function
    
    ErrHandler:
        Err.Clear
        On Error GoTo 0
        getSmtpMailAddress = "???"
    End Function
    

    This works for Outlook 2007. The MSDN solution for Outlook 2010 as pointed out above, looks a bit nicer.

    0 讨论(0)
提交回复
热议问题