简体   繁体   中英

In Outlook Addin, how do I determine if an email's Sender is a shared mailbox email address?

In my Outlook Addin, I need to know if the Sender email adddress in the From field is a type of shared mailbox email address before allowing the email to be sent out. Currently, I'm checking that if the sender address doesn't exist in one of the current sessions' accounts' email address, I would assume it as a shared mailbox email address because it is not possible for a person to login to Outlook using a shared mailbox email address.

However, this logic is flawed because non-shared mailbox email address can also be set in the From field (Correct me if I'm wrong).

Here is my code:

    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;
    }

Is there a better way to determine whether the Sender email address in the From field is a shared mailbox email address or not?

Thanks to @DmitryStreblechenko who gave me the idea to look into AutoDiscoverXml in the comments . Indeed, in AutoDiscoverXml, there are nodes called AlternativeMailbox which seems to indicate the list of shared mailboxes that the account user is a part of. Querying this XML nodes will allow us to extract the email address of the shared mailbox from two child nodes called SmtpAddress and OwnerSmtpAddress .

I'm not sure about the difference between those two but in this case, I'm using 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM