use*_*019 6 c# vsto outlook-addin
关于这个已经有几个问题了,但我面临着一个不同的问题。在其他问题上发布的解决方案对我不起作用,我怀疑原因是我正在尝试获取外发电子邮件的发件人电子邮件地址,而不是从其他人发送并位于邮件文件夹中的电子邮件地址.
简而言之,我正在编写一个 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
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里尝试了解决方案:https : //docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-the-smtp-address-of-the-sender-of-邮件项目。这不起作用,而是给出了 NullReferenceException。
我也确实假设默认邮箱是发件人的电子邮件地址,但这对于拥有多个邮箱的人来说是个坏主意。
我认为唯一的解决方案(第 3 方插件除外)是遍历帐户上的每个邮箱,获取 X400 地址(如果交换邮箱)并放入包含电子邮件地址的数组。发送电子邮件时,从外发电子邮件中获取 X400,将其与帐户的 X400 匹配,然后我将获得电子邮件地址。不知道这是否可能,甚至是一个不错的解决方案。
我必须找到解决办法。我仍在GetSenderSMTPAddress问题中使用该函数,但注意到GetSenderSMTPAddress如果它是辅助邮箱,则它将正常工作,如果它是主邮箱,则将为空,在这种情况下,您可以使用mailItem.SendUsingAccount.SmtpAddress.
if (GetSenderSMTPAddress(mailItem) != null) // secondary or additional mailbox
{
sendingAddress = GetSenderSMTPAddress(mailItem);
}
else //primary mailbox
{
sendingAddress = mailItem.SendUsingAccount.SmtpAddress;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
254 次 |
| 最近记录: |