我在尝试获取电子邮件中的其他电子邮件地址列表时遇到了一些问题。目前它获得 SenderName 就好了,但一旦转移到 To ,它似乎仍然一遍又一遍 SenderName
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
现在,正如我所说的 SenderName 一样正常。但是,下面的代码是上面代码之后的代码:
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 => DL@zzzz.com
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 ,我知道这将始终生成 SenderName ,但这是我不确定如何获取< strong> TO,CC&amp; BCC
我试过了:
GetSenderSMTPAddress(_mailItem.To)
并发出警告:
警告1转换&#39; String&#39;时可能会出现运行时错误。至 &#39; Microsoft.Office.Interop.Outlook.MailItem&#39;
所以解决这个问题的任何提示都会很棒!
答案 0 :(得分:0)
所有收件人都作为收件人对象存储在MailItem.Recipients中。您需要访问Recipient.AddressEntry才能获取Exchange收件人的SMTP地址。 To,Cc和Bcc字段仅对获取显示名称有用。
另外:没有必要在SenderName上进行拆分;它始终是一个电子邮件地址或显示名称。