2014-07-16 45 views
0

我已經有一些問題想收到一封電子郵件中其他電子郵件地址的列表。目前它獲得SenderName就好,但一旦它移動到它似乎仍然有SenderName一遍又一遍。Outlook插件發件人/ TO/CC/BCC問題VB.net

 If _mailItem.SenderName IsNot Nothing Then 
     Dim tmpResult() As String = _mailItem.SenderName.ToString.Split(";") 

     For Each name In tmpResult 
      result = name.Split(",") 

      If result.Length > 1 Then 
       employeeAlreadyThere = EIG.FindContactEmailByName(GetSenderSMTPAddress(_mailItem).Trim) 

       If employeeAlreadyThere = False Then 
        Call EIG.Search(name.ToString) 
       End If 
      End If 
     Next 
    End If 

現在,作爲我的發送者姓名說,工作得很好。但是,下面的代碼是上面的代碼後,會發生什麼:

If _mailItem.To IsNot Nothing Then 
     Dim tmpResult() As String = _mailItem.To.ToString.Split(";") 

     For Each name In tmpResult 
      result = name.Split(",") 

      If result.Length > 1 Then 
       employeeAlreadyThere = EIG.FindContactEmailByName(GetSenderSMTPAddress(_mailItem).Trim) 

       If employeeAlreadyThere = False Then 
        Call EIG.Search(name.ToString) 
       End If 
      Else 
       EIG.lastFirstName = name.Trim().ToString 
       EIG.emailAddress = EIG.lastFirstName.Replace("-", "_").Replace("/", "").Replace("\", "").Replace(" ", "_").Trim() & "@zzzz.com" 'DL-I/S etwBusiness => [email protected] 
       Call EIG.saveImage() 
      End If 
     Next 
    End If 

GetSenderSMTPAddress(_mailItem)代碼是這樣的:

Private Function GetSenderSMTPAddress(ByVal 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 mail_sender As Outlook.AddressEntry = mail.Sender 

     If mail_sender IsNot Nothing Then 
      If mail_sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry OrElse mail_sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then 
       Dim exchUser As Outlook.ExchangeUser = mail_sender.GetExchangeUser() 

       If exchUser IsNot Nothing Then 
        Return exchUser.PrimarySmtpAddress 
       Else 
        Return Nothing 
       End If 
      Else 
       Return TryCast(mail_sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String) 
      End If 
     Else 
      Return Nothing 
     End If 
    Else 
     Return mail.SenderEmailAddress 
    End If 
End Function 

我傳遞一個Outlook.MailItem和我知道,總是會產生發送者姓名但是這是我不能確定如何去得到TO,CC & BCC

我已經試過:

GetSenderSMTPAddress(_mailItem.To) 

和拋出的警告:

警告1個運行時錯誤轉換 '字符串' 到 「Microsoft.Office.Interop.Outlook時可能發生.MailItem」。

因此,對解決這個任何提示將是巨大的!

回答

0

所有收件人都存儲在MailItem.Recipients爲收件人對象。您將需要訪問Recipient.AddressEntry以獲取Exchange收件人的SMTP地址。 「收件人」,「抄送」和「密件抄送」字段僅用於獲取顯示名稱。

另外:有沒有必要做的發送者姓名分流;它將始終是一個電子郵件地址或顯示名稱。

+0

如果我這樣做** Dim mail_sender As Outlook.AddressEntry = mail.To **而不是** Dim mail_sender As Outlook.AddressEntry = mail.Sender **它給出Warning *運行時錯誤可能發生在轉換'String '到‘Microsoft.Office.Interop.Outlook.AddressEntry’。* – StealthRT

+0

這是因爲要屬性返回一個字符串,而不是一個AddressEntry對象。只有Recipient.AddressEntry和MailItem.Sender纔會返回AddressEntry對象。 –

+0

那麼我將如何去獲取To/CC和BCC? – StealthRT