Search code examples
c#outlookvstooutlook-addin

How can I get the account associated to a meeting request?


I need to know the account email address a meeting request is associated to (the email address the meeting request is sent to):

string GetAssociatedAccountEmailAddress(Outlook.MeetingItem meetingItem)
{
    //TODO: implement this method:
    throw new NotImplementedException();
}

This is what I tried:

string GetAssociatedAccountEmailAddress1(Outlook.MeetingItem meetingItem)
{
    Outlook.MAPIFolder folder = meetingItem.Parent;
    Debug.WriteLine("Folder Name: {0}, Folder Path: {1}", folder.Name, folder.FolderPath);

    Outlook.MAPIFolder folderParent = folder.Parent;
    Debug.WriteLine("Folder Parent Name: {0}, Folder Parent Path: {1}", folderParent.Name, folderParent.FolderPath);

    return folderParent.FolderPath.Replace("\\", "");
}

Debug output:

Folder Name: Inbox, Folder Path: \\\\[email protected]\Inbox
Folder Parent Name: [email protected], Folder Parent Path: \\\\[email protected]

The problem with this implementation is that I'm not sure the folder path will always contain the email address.

I also tried the following:

string GetAssociatedAccountEmailAddress2(Outlook.MeetingItem meetingItem)
{
    Outlook.MAPIFolder folder = meetingItem.Parent;

    Outlook.MAPIFolder folderParent = folder.Parent;

    Outlook.NameSpace ns = folderParent.Parent;
    return ns.Accounts.Cast<Outlook.Account>()
        .FirstOrDefault(x => meetingItem.Recipients.Cast<Outlook.Recipient>().Any(r => r.Address == x.SmtpAddress))
        .SmtpAddress;
}

The problem with this is that if I have two accounts ([email protected] and [email protected]) and the meeting request is sent to both, then I have two meeting requests but GetAssociatedAccountEmailAddress2 returns the same email address.

FYI: I'm developing an Outlook add-in for Outlook 2013 using VS 2015.


Solution

  • Couple ways to do that -

    1. Read the PR_RECEIVED_BY_ENTRYID property (not guaranteed to be present, DASL name http://schemas.microsoft.com/mapi/proptag/0x003F0102) using MeetingItem.PropertyAccessor.GetProperty, convert it to a hex string using PropertyAccessor.BinaryToString, use it to call Application.Session.GetAddressEntryFromID. Note that the property might not match the actual store owner if the item was copied from another store. Take a look at the meeting request with OutlookSpy (I am its authot) - click IMessage button to see that property.

    2. Read the PR_MAILBOX_OWNER_ENTRYID property (DASL name http://schemas.microsoft.com/mapi/proptag/0x661B0102) from the parent store (MeetingItem.Parent.Store) using Store.PropertyAccessor.GetProperty. The property is not guaranteed to be present. If using Redemption (I am also its author) is an option, it exposes the RDOExchangeMailboxStore object that has the Owner property (returns RDOAddressEntry object).