如何在 VB.NET 中使用 Outlook.MailItem 获取发件人电子邮件地址? [英] How can I get the sender email address using Outlook.MailItem in VB.NET?
问题描述
我曾尝试使用 mailItem.SenderEmailAddress
和 mailItem.Sender.Address
但它们都返回一个如下所示的字符串:
I have tried using mailItem.SenderEmailAddress
and mailItem.Sender.Address
but they both return a string that looks like this:
/O=DOMAINNAME/OU=EXCHANGE 管理组 (FYDIBOHI43SPCLT)/CN=RECIPIENTS/CN=JOE BLOGGS8C3
实际上我希望 joebloggs@domainname.co.uk
被退回.
Where in reality I want joebloggs@domainname.co.uk
to be retrurned.
有人有什么想法吗?
非常感谢.
我做了一些挖掘;它适用于SenderEmailType"SMTP 的电子邮件地址,但不适用于 Exchange 电子邮件地址.
I have done some digging; it works perfectly for email addresses of the 'SenderEmailType' SMTP, it just doesn't work for Exchange email addresses.
编辑 2:我尝试了指定的代码 此处,但我认为它已过时,因为它会引发无法创建 Active-X 组件"错误.
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.
编辑 3:对于和我有同样问题的人,我找到了答案(在 C# 中,转换为 VB.NET,但仍然有效):
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):
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
推荐答案
我看到你已经回答了你自己的问题.我将在这里发布我的 C# 函数,以防有人需要它,或者如果您想将其用作更多帮助.我的 C# 函数用于执行您的操作,如下所示:
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:
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;
}
这篇关于如何在 VB.NET 中使用 Outlook.MailItem 获取发件人电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!