is there a way to get the smtpaddress for an mapifolder or outlook interop folder given the entryid (or storeid)

邮差的信 提交于 2020-02-25 13:19:10

问题


if i have the storeid for a mapifolder, selected through the folderpicker from the outlook interop libraries, is there a way for me to get the smtpaddress for that folder?

i know it's in the extended properties, but i was hoping to do it without any heavy parsing or ldap querying.

the reason i need the smtpaddress is in order to connect to the folder via EWS - i'm currently trying to replace our references to outlook interop with exchange web services, and this has become a sticking point, since many of our users have delegate access to mailboxes that don't belong to them


回答1:


For the mailbox owner, you can either try to read the MAPIFolder.Store property to get to the parent store, then read the PR_MAILBOX_OWNER_ENTRYID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x661B0102") using Store.PropertyAccessor.GetProperty. You can then use the store owner entry id to call Namespace.GetAddressEntryFromID. Once you have the AddressEntry object, you can use AddressEntry.GetExchangeUser.PrimarySmtpAddress.

Note that PR_MAILBOX_OWNER_ENTRYID property is only available in the online stores. You might want to use Redemption and its RDOExchangeMailboxStore.Owner.SmtpAddress property. RDOExchangeMailboxStore can be retrieved using RDOSession.GetRDOObjectfromOutlookObject(Store) or using RDOSession.GetStoreFromID.




回答2:


I know it's years later (sorry), but I needed get SMTP addresses for a bunch of mailboxes, and the accepted answer didn't work (because I've got offline stores) so I did the parsing.

public static bool TryGetSmtpAddress(MAPIFolder folder, out string smtpAddress)
{
    smtpAddress = default;

    var storeId = HexToBytes(folder.StoreID);

    // check it's a store entry id
    if (BitConverter.ToUInt64(storeId, 4) != 0x1A10E50510BBA138UL
        || BitConverter.ToUInt64(storeId, 12) != 0xC2562A2B0008BBA1UL) { return false; }

    var indexDn = Array.IndexOf(storeId, (byte)0x00, 60) + 1;
    var indexV3Block = Array.IndexOf(storeId, (byte)0x00, indexDn) + 1;

    // check it's a V3 entry id (with SMTP address)
    if (BitConverter.ToUInt32(storeId, indexV3Block) != 0xF43246E9UL) { return false; }

    var offsetSmtpAddress = BitConverter.ToUInt32(storeId, indexV3Block + 12);

    smtpAddress = BytesToUnicode(storeId, indexV3Block + (int)offsetSmtpAddress);
    return true;
}

private static byte[] HexToBytes(string input)
{
    var bytesLength = input.Length / 2;
    var bytes = new byte[bytesLength];
    for (var i = 0; i < bytesLength; i++) { bytes[i] = Convert.ToByte(input.Substring(i * 2, 2), 16); }
    return bytes;
}

private static string BytesToUnicode(byte[] value, int startIndex)
{
    var charsLength = (value.Length - startIndex) / 2;
    var chars = new char[charsLength];
    for (var i = 0; i < charsLength; i++)
    {
        var c = chars[i] = BitConverter.ToChar(value, startIndex + i * 2);
        if (c == '\0') { return new String(chars, 0, i); }
    }
    return new String(chars);
}


来源:https://stackoverflow.com/questions/21443597/is-there-a-way-to-get-the-smtpaddress-for-an-mapifolder-or-outlook-interop-folde

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!