在 Outlook Addin 中,如何确定电子邮件的发件人是否为共享邮箱电子邮件地址?
In Outlook Addin, how do I determine if an email's Sender is a shared mailbox email address?
在我的 Outlook 插件中,在允许发送电子邮件之前,我需要知道发件人字段中的发件人电子邮件地址是否是一种共享邮箱电子邮件地址。目前,我正在检查如果发件人地址不存在于当前会话帐户的电子邮件地址之一中,我会假设它是一个共享邮箱电子邮件地址,因为一个人不可能使用登录到 Outlook共享邮箱电子邮件地址。
但是,这个逻辑是有缺陷的,因为非共享邮箱的电子邮件地址也可以设置在发件人字段中(如果我错了,请纠正我)。
这是我的代码:
public bool isSharedMailboxAccount(Microsoft.Office.Interop.Outlook.MailItem mail)
{
string fromEmail = mail.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E").ToString();
bool isShared = true;
foreach (Microsoft.Office.Interop.Outlook.Account acc in mail.Session.Accounts)
{
if (acc.SmtpAddress == fromEmail)
{
isShared = false;
break;
}
}
return isShared;
}
有没有更好的方法来判断发件人字段中的发件人电子邮件地址是否为共享邮箱电子邮件地址?
感谢@DmitryStreblechenko 让我想到在评论中查看 AutoDiscoverXml。事实上,在 AutoDiscoverXml 中,有一些名为 AlternativeMailbox
的节点,它似乎表示帐户用户所属的共享邮箱列表。查询此 XML 节点将使我们能够从名为 SmtpAddress
和 OwnerSmtpAddress
.
的两个子节点中提取共享邮箱的电子邮件地址
我不确定这两者之间的区别,但在这种情况下,我使用的是 SmtpAddress
。
public bool isSharedMailboxAccount(Microsoft.Office.Interop.Outlook.MailItem mail)
{
string fromEmail = mail.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E").ToString();
bool isShared = false;
foreach (Microsoft.Office.Interop.Outlook.Account acc in mail.Session.Accounts)
{
//using XMLDocument to query AutoDiscoverXML value
XmlDocument xml = new XmlDocument();
xml.LoadXml(account.autoDiscoverXml);
XmlNodeList alts = xml.GetElementsByTagName("AlternativeMailbox");
foreach (XmlNode alt in alts)
{
//NOTE: I'm not entirely sure that alt mailboxes that has Type == Delegate will always mean shared mailbox but so far in my tests, it seems to be so.
if(alt.ChildNodes.OfType<XmlElement>().Where(e => e.LocalName == "Type").First().InnerText.Equals("Delegate"))
{
if(alt.ChildNodes.OfType<XmlElement>().Where(e => e.LocalName == "SmtpAddress").First().InnerText.Equals(fromEmail))
{
isShared = true;
break;
}
}
}
if(isShared)
{
break;
}
}
return isShared;
}
在我的 Outlook 插件中,在允许发送电子邮件之前,我需要知道发件人字段中的发件人电子邮件地址是否是一种共享邮箱电子邮件地址。目前,我正在检查如果发件人地址不存在于当前会话帐户的电子邮件地址之一中,我会假设它是一个共享邮箱电子邮件地址,因为一个人不可能使用登录到 Outlook共享邮箱电子邮件地址。
但是,这个逻辑是有缺陷的,因为非共享邮箱的电子邮件地址也可以设置在发件人字段中(如果我错了,请纠正我)。
这是我的代码:
public bool isSharedMailboxAccount(Microsoft.Office.Interop.Outlook.MailItem mail)
{
string fromEmail = mail.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E").ToString();
bool isShared = true;
foreach (Microsoft.Office.Interop.Outlook.Account acc in mail.Session.Accounts)
{
if (acc.SmtpAddress == fromEmail)
{
isShared = false;
break;
}
}
return isShared;
}
有没有更好的方法来判断发件人字段中的发件人电子邮件地址是否为共享邮箱电子邮件地址?
感谢@DmitryStreblechenko 让我想到在评论中查看 AutoDiscoverXml。事实上,在 AutoDiscoverXml 中,有一些名为 AlternativeMailbox
的节点,它似乎表示帐户用户所属的共享邮箱列表。查询此 XML 节点将使我们能够从名为 SmtpAddress
和 OwnerSmtpAddress
.
我不确定这两者之间的区别,但在这种情况下,我使用的是 SmtpAddress
。
public bool isSharedMailboxAccount(Microsoft.Office.Interop.Outlook.MailItem mail)
{
string fromEmail = mail.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E").ToString();
bool isShared = false;
foreach (Microsoft.Office.Interop.Outlook.Account acc in mail.Session.Accounts)
{
//using XMLDocument to query AutoDiscoverXML value
XmlDocument xml = new XmlDocument();
xml.LoadXml(account.autoDiscoverXml);
XmlNodeList alts = xml.GetElementsByTagName("AlternativeMailbox");
foreach (XmlNode alt in alts)
{
//NOTE: I'm not entirely sure that alt mailboxes that has Type == Delegate will always mean shared mailbox but so far in my tests, it seems to be so.
if(alt.ChildNodes.OfType<XmlElement>().Where(e => e.LocalName == "Type").First().InnerText.Equals("Delegate"))
{
if(alt.ChildNodes.OfType<XmlElement>().Where(e => e.LocalName == "SmtpAddress").First().InnerText.Equals(fromEmail))
{
isShared = true;
break;
}
}
}
if(isShared)
{
break;
}
}
return isShared;
}