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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24361726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:32:14  来源:igfitidea点击:

How can I get the sender email address using Outlook.MailItem in VB.NET?

vb.netemailoutlookoutlook-addinmailitem

提问by ubergorp

I have tried using mailItem.SenderEmailAddressand mailItem.Sender.Addressbut they both return a string that looks like this:

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

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

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

Where in reality I want [email protected]to be retrurned.

在现实中我想[email protected]被退回的地方。

Anyone have any ideas?

谁有想法?

Thank you very much.

非常感谢。

Edit: I have done some digging; it works perfectly for email addresses of the 'SenderEmailType' SMTP, it just doesn't work for Exchange email addresses.

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

Edit 2: I have tried the code specified here, but I assume it is outdated because it throws a "Cannot create Active-X component" error.

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

EDIT 3:For anyone who ever has the same problem as me, I found the answer (in C#, converted to VB.NET, still works though):

编辑 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

回答by Alex Harvey

I see you have answered your own question. I will post my C# function here incase anybody needs it or if you would like to use it as more help. My C# function for doing what you do looks like this:

我看到你已经回答了你自己的问题。我将在这里发布我的 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;
}

回答by prgSRR

If someone's still looking for a solution to this problem, here is a simplified and true-blue VBA version of the code to handle this requirement.

如果有人仍在寻找这个问题的解决方案,这里有一个简化的、纯正的 VBA 版本的代码来处理这个需求。

Public Sub GetCurrentItem()
    On Error Resume Next
    Set ObjSelectedItem = Outlook.ActiveExplorer.Selection.Item(1)
    If TypeName(ObjSelectedItem) = "MailItem" Then
        If ObjSelectedItem.SenderEmailType = "EX" Then
            MsgBox (ObjSelectedItem.Sender.GetExchangeUser.PrimarySmtpAddress)
        Else
            MsgBox (ObjSelectedItem.SenderEmailAddress)
        End If
    Else
        MsgBox ("No items selected (OR) Selected item not a MailItem.")
    End If
    Set ObjSelectedItem = Nothing
End Sub

回答by Gerry Wilton

VBA Solution as well (Just translated the VB.net)

VBA 解决方案也是如此(刚刚翻译了 VB.net)

Private Function GetSenderSMTPAddress(mail As Outlook.MailItem) As String

私有函数 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

End Function

结束函数

回答by larcy

To me a much simpler answer is as follows

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

Where to get an external address you may have used SenderEmailAddress, then for an internal (i.e. from exchange) address use Sender.GetExchangeUser.PrimartySmtpAdress instead

在哪里获取您可能使用过 SenderEmailAddress 的外部地址,然后对于内部(即来自交换)地址,请改用 Sender.GetExchangeUser.PrimartySmtpAdress

If you want it to work for both internal and external addresses then put in a test to see whether the address was internal or external first. Example code snippet below

如果您希望它同时适用于内部和外部地址,请先进行测试以查看地址是内部地址还是外部地址。下面的示例代码片段

If itm.SenderEmailType = "SMTP" Then
        mailfrom = itm.SenderEmailAddress
Else
If itm.SenderEmailType = "EX" Then
        mailfrom = itm.Sender.GetExchangeUser.PrimarySmtpAddress
End If
End If

回答by Abdul Rehman

Created a VBA function if you wanted to use it for simplicity. A sample call would be Left(GetEmailAddress(mai) & Space(50), 50)where maiis expected to be a MailItemobject. Used and tested successfully in Microsoft Outlook 2010

如果您想简单使用它,请创建一个 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