如何在VB.NET中使用Outlook.MailItem获取发件人电子邮件地址?

时间:2014-06-23 08:44:21

标签: vb.net email outlook outlook-addin mailitem

我尝试使用mailItem.SenderEmailAddressmailItem.Sender.Address,但它们都返回一个如下所示的字符串:

/O=DOMAINNAME/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHI43SPCLT)/CN=RECIPIENTS/CN=JOE BLOGGS8C3

实际上我希望joebloggs@domainname.co.uk被重新发布。

有人有什么想法吗?

非常感谢。

编辑:我做了一些挖掘;它适用于“SenderEmailType”SMTP的电子邮件地址,它只适用于Exchange电子邮件地址。

编辑2:我已经尝试了指定here的代码,但我认为它已过时,因为它会抛出“无法创建Active-X组件”错误。

编辑3: 对于那些遇到和我一样问题的人,我找到了答案(在C#中,转换为VB.NET,但仍然有效):

Private Function GetSenderSMTPAddress(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 sender As Outlook.AddressEntry = mail.Sender
        If sender IsNot Nothing Then
            'Now we have an AddressEntry representing the Sender
            If sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry OrElse sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then
                'Use the ExchangeUser object PrimarySMTPAddress
                Dim exchUser As Outlook.ExchangeUser = sender.GetExchangeUser()
                If exchUser IsNot Nothing Then
                    Return exchUser.PrimarySmtpAddress
                Else
                    Return Nothing
                End If
            Else
                Return TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String)
            End If
        Else
            Return Nothing
        End If
    Else
        Return mail.SenderEmailAddress
    End If
End Function

5 个答案:

答案 0 :(得分:23)

我看到你已回答了自己的问题。我会在这里发布我的C#函数,任何人都需要它,或者如果你想用它作为更多的帮助。我做你做的事情的C#函数看起来像这样:

 private string getSenderEmailAddress(Outlook.MailItem mail)
{
 Outlook.AddressEntry sender = mail.Sender;
 string SenderEmailAddress = "";

  if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
    {
        Outlook.ExchangeUser exchUser = sender.GetExchangeUser();
        if (exchUser != null)
        {
            SenderEmailAddress = exchUser.PrimarySmtpAddress;
        }
    }
    else
    {
        SenderEmailAddress = mail.SenderEmailAddress;
    }

    return SenderEmailAddress;
}

答案 1 :(得分:6)

VBA解决方案(刚刚翻译成VB.net)

私有函数GetSenderSMTPAddress(mail as Outlook.MailItem)As String

If mail Is Nothing Then
    GetSenderSMTPAddress = vbNullString
    Exit Function
End If
If mail.SenderEmailType = "EX" Then
    Dim sender As Outlook.AddressEntry
    Set sender = mail.sender
    If Not sender Is Nothing Then
        'Now we have an AddressEntry representing the Sender
        If sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry Or sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then
            'Use the ExchangeUser object PrimarySMTPAddress
            Dim exchUser As Outlook.ExchangeUser
            Set exchUser = sender.GetExchangeUser()
            If Not exchUser Is Nothing Then
                 GetSenderSMTPAddress = exchUser.PrimarySmtpAddress
            Else
                GetSenderSMTPAddress = vbNullString
            End If
        Else
             GetSenderSMTPAddress = sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
        End If
    Else
        GetSenderSMTPAddress = vbNullString
    End If
Else
    GetSenderSMTPAddress = mail.SenderEmailAddress
End If

结束功能

答案 2 :(得分:3)

对我来说,一个更简单的答案如下

在哪里获得外部地址,您可能使用过SenderEmailAddress,然后使用Sender.GetExchangeUser.PrimartySmtpAdress代替

进行内部(即来自交换)地址

如果您希望它对内部和外部地址都有效,那么请先进行测试,看看该地址是内部地址还是外部地址。

下面的示例代码段
If itm.SenderEmailType = "SMTP" Then
        mailfrom = itm.SenderEmailAddress
Else
If itm.SenderEmailType = "EX" Then
        mailfrom = itm.Sender.GetExchangeUser.PrimarySmtpAddress
End If
End If

答案 3 :(得分:0)

在 C# 中,您可以使用 Outlook MailItem 的 SendUsingAccount.SmtpAddress 属性访问发件人的电子邮件地址。它返回一个字符串对象。 VB.net 应该是类似的。

string sender = mail.SendUsingAccount.SmtpAddress;

其中 mail 是 Outlook.MailItem

答案 4 :(得分:-1)

如果您想使用它来简化,请创建一个VBA函数。样本调用将是     Left(GetEmailAddress(mai) & Space(50), 50) 其中mai应为MailItem个对象。在Microsoft Outlook 2010

中成功使用和测试
Public Function GetEmailAddress(mai As Object) As String
    On Error Resume Next
    Set ObjSelectedItem = mai
    If TypeName(ObjSelectedItem) = "MailItem" Then
        If ObjSelectedItem.SenderEmailType = "EX" Then
            GetEmailAddress = ObjSelectedItem.Sender.GetExchangeUser.PrimarySmtpAddress
        Else
            GetEmailAddress = ObjSelectedItem.SenderEmailAddress
        End If
    Else
        GetEmailAddress = "Not a MailItem"
    End If

    Set ObjSelectedItem = Nothing
    End Function