24

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

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

在现实中我想joebloggs@domainname.co.uk被退回的地方。

有人有想法么?

非常感谢。

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

编辑 2:我已经尝试过这里指定的代码,但我认为它已经过时,因为它会引发“无法创建 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
4

6 回答 6

34

我看到你已经回答了你自己的问题。我将在此处发布我的 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;
}
于 2014-07-02T15:35:12.473 回答
9

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

Private Function 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
于 2014-10-03T01:16:51.153 回答
9

如果有人仍在寻找解决这个问题的方法,这里有一个简化的、真正的蓝色 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
于 2015-08-26T12:37:11.477 回答
8

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

在哪里获取您可能使用过 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
于 2017-07-10T16:50:43.900 回答
1

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

string sender = mail.SendUsingAccount.SmtpAddress;

邮件在哪里Outlook.MailItem

于 2020-12-29T14:34:14.427 回答
-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
于 2016-10-07T15:55:29.430 回答