在这方面已经有几个问题,但我面临的是另一个问题。在其他问题上发布的解决方案对我无效,我怀疑原因是我试图获取发送者的电子邮件地址,而不是从其他人发送的电子邮件地址,并且位于邮件文件夹中。
我想做的是
简单地说,我正在编写一个Outlook插件,它连接到"ItemSend“事件,并运行一个函数来显示发送者的电子邮件(SMTP)地址,当他们单击电子邮件上的”发送“。
问题
当从Exchange邮箱发送电子邮件时,我无法获得SMTP地址。相反,mail.SenderEmailAddresss
提供一个X400地址,我发现的其他方法要么给出异常,要么根本不返回电子邮件地址。
GetSenderSMTPAddress(mail)
提供空白输出
mail.SenderEmailAddresss
的结果是:/o=Company Organisation/ou=Exchange Administrative Group (ABC123T)/cn=Recipients/cn=abc123-
mail.Sender.Address
导致Exception thrown: 'System.NullReferenceException' in OutlookTesting.dll
我现在的密码
namespace OutlookTesting
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)Item;
Debug.WriteLine("Using GetSenderSMTPAddress function: " + GetSenderSMTPAddress(mail));
Debug.WriteLine("Using mail.SenderEmailAddresss: " + mail.SenderEmailAddress);
Debug.WriteLine("Using mail.Sender.Address: " + mail.Sender.Address);
}
}
private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
string PR_SMTP_ADDRESS =
@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
if (mail == null)
{
throw new ArgumentNullException();
}
if (mail.SenderEmailType == "EX")
{
Outlook.AddressEntry sender = mail.Sender;
if (sender != null)
{
//Now we have an AddressEntry representing the Sender
if (sender.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeUserAddressEntry
|| sender.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeRemoteUserAddressEntry)
{
//Use the ExchangeUser object PrimarySMTPAddress
Outlook.ExchangeUser exchUser =
sender.GetExchangeUser();
if (exchUser != null)
{
return exchUser.PrimarySmtpAddress;
}
else
{
return null;
}
}
else
{
return sender.PropertyAccessor.GetProperty(
PR_SMTP_ADDRESS) as string;
}
}
else
{
return null;
}
}
else
{
return mail.SenderEmailAddress;
}
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
}
#endregion
}
}
其他我试过的事情
我在这里尝试了解决方案:https://learn.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-the-smtp-address-of-the-sender-of-a-mail-item。这不起作用,反而给出了一个NullReferenceException。
我也回到了假设默认邮箱是发送者电子邮件地址的假设,但是对于那些有多个邮箱的人来说,这是个坏主意。
闭幕式思想
我认为唯一的解决方案(第三方插件除外)将是循环遍历帐户上的每个邮箱,获取X400地址(如果交换邮箱)并放入一个带有电子邮件地址的数组。当一封电子邮件被发送时,从发送出去的电子邮件中获取X400,将其与帐户的X400匹配,然后我将有电子邮件地址。不过,目前还不确定这是否可能,甚至是一个像样的解决方案。
发布于 2020-12-09 06:19:49
我得在附近找份工作。我仍然在问题中使用GetSenderSMTPAddress
函数,但注意到如果它是次要邮箱,GetSenderSMTPAddress
会正常工作,如果它是主邮箱,则为null,在这种情况下,您可以使用mailItem.SendUsingAccount.SmtpAddress
。
if (GetSenderSMTPAddress(mailItem) != null) // secondary or additional mailbox
{
sendingAddress = GetSenderSMTPAddress(mailItem);
}
else //primary mailbox
{
sendingAddress = mailItem.SendUsingAccount.SmtpAddress;
}
发布于 2020-12-07 15:34:09
这对我有用,希望它能帮上忙:
Microsoft.Office.Interop.Outlook.MailItem mailItem = Item as MailItem;
string smtp = mailItem.SendUsingAccount.SmtpAddress;
https://stackoverflow.com/questions/65006384
复制