如何使用VB.NET中的Outlook.MailItem获取发件人电子邮件地址? [英] How can I get the sender email address using Outlook.MailItem in VB.NET?
问题描述
我尝试使用 mailItem.SenderEmailAddress
和 mailItem.Sender.Address
,但它们都返回一个字符串,看起来像这样:
/ O = DOMAINNAME / OU = EXCHANGE行政组织(FYDIBOHI43SPCLT)/ CN = RECIPIENTS / CN = JOE BLOGGS8C3
实际上,我想要 joebloggs@domainname.co.uk
被翻译。 >
任何人都有任何想法?
非常感谢。
<编辑:我做了一些挖掘;它完美地适用于SenderEmailTypeSMTP的电子邮件地址,它不适用于Exchange电子邮件地址。
编辑3:
对于任何与我有同样问题的人,我发现答案(在C#中,转换为VB.NET,仍然可以使用):
私有函数GetSenderSMTPAddress(邮件作为Outlook.MailItem)As String
Dim PR_SMTP_ADDRESS As String =http://schemas.microsoft.com/mapi/proptag/0x39FE001E
如果邮件是Nothing然后
抛出新的ArgumentNullException()
结束如果
如果mail.SenderEmailType =EX然后
Dim发送者作为Outlook.AddressEntry = mail.Sender
如果发件人IsNot Nothing然后
'现在我们有一个AddressEntry表示发件人
如果sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry OrElse sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry然后
'使用ExchangeUser对象PrimarySMTPAddress
Dim exchUser As Outlook.ExchangeUser = sender.GetExchangeUser()
如果exchUser IsNot Nothing然后
返回exchUser.PrimarySmtpAddress
Else
返回没有
如果
Else
返回TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS),String)
End If
Else
返回无
结束If
Else
返回mail.SenderEmailAddress
结束如果
结束函数
我看到你已经回答了自己的问题。我会在这里发布我的C#函数,而不用任何人需要它,或者如果你想使用它更多的帮助。我的C#函数做你做的事情看起来像这样:
私有字符串getSenderEmailAddress(Outlook.MailItem邮件)
{
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;
}
I have tried using mailItem.SenderEmailAddress
and mailItem.Sender.Address
but they both return a string that looks like this:
/O=DOMAINNAME/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHI43SPCLT)/CN=RECIPIENTS/CN=JOE BLOGGS8C3
Where in reality I want joebloggs@domainname.co.uk
to be retrurned.
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.
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.
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
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屋!