0

I'm trying to use Redemption and Profman to add an Exchange Online mailbox to an existing Outlook profile. I want to simulate the same result as when you use File | Add Account in Outlook. (I don't want to add a delegate mailbox.)

I've written some code (it's below), but I can't get it to work. I am testing with an email account that I have no problem adding manually. When I use the File | Add Account in Outlook, a screen pops up that asks me for an email address. I enter the email address, and the account is successfully added to my profile. I don't even need to enter a password.

However, when I use the code below for the same email address, it doesn't work. The mailbox shows up in Outlook, but when I click on it, I get the message: "The set of folders cannot be opened. The attempt to log on to Microsoft Exchange has failed". And there is no prompt for me to enter a password.

I'm using Outlook 2021. The code below was adapted from: https://www.dimastr.com/redemption/profman_examples.htm#ROH_Profile_Outlook2016

private static void AddExoMailbox(string profileName, string email)
{
    const int PR_DISPLAY_NAME_W = 0x3001001F;
    const int PR_PROFILE_USER_SMTP_EMAIL_ADDRESS_W = 0x6641001F;
    const int PR_EMSMDB_SECTION_UID = 0x3D150102;
    const int PR_STORE_PROVIDERS = 0x3D000102;

    var profile = new ProfMan.Profiles().Item[profileName];

    var ExchService = profile.Services.Add("MSEMS", "Microsoft Exchange", false);
    var vProfileGuid = ExchService.ProfSect.Item[PR_EMSMDB_SECTION_UID];

    dynamic ProviderStoreUid = null;
    for (int k = 1; k <= ExchService.Providers.Count; k++)
    {
        var ExProvider = ExchService.Providers.Item[k];
        var ProviderUid = ExProvider.UID;

        if (ProviderUid == vProfileGuid)
        {
            var ProviderProfSect = ExProvider.ProfSect;
            ProviderProfSect.Item[PR_PROFILE_USER_SMTP_EMAIL_ADDRESS_W] = $"SMTP:{email}";

            ProviderStoreUid = ExchService.ProfSect.Item[PR_STORE_PROVIDERS];
        }
    }

    for (int k = 1; k <= ExchService.Providers.Count; k++)
    {
        var ExProvider = ExchService.Providers.Item[k];
        var ProviderUid = ExProvider.UID;
        if (ProviderUid == ProviderStoreUid)
        {
            var StoreProviderProfSect = ExProvider.ProfSect;
            StoreProviderProfSect.Item[PR_DISPLAY_NAME_W] = email;
            StoreProviderProfSect.Item[PR_PROFILE_USER_SMTP_EMAIL_ADDRESS_W] = $"SMTP:{email}";
        }
    }
}

Any pointers?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
John D
  • 79
  • 1
  • 5
  • Why are you prefixing the SMTP address with `SMTP:`? – Dmitry Streblechenko Jun 27 '22 at 21:47
  • Also, have you tried to compare newly added account properties in OutlookSpy to that created by Outlook? – Dmitry Streblechenko Jun 28 '22 at 00:03
  • Yes, I did use OutlookSpy to compare the one added manually and the one added programatically. Aside from different UIDs, I saw no differences in the IMsgServiceAdmin. Is there anywhere else worth comparing in OutlookSpy? I added the `SMTP:` because it was used in the link below. I tested with both `SMTP:` and without and got same results. https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/how-to-programmatically-create-a-profile-in-outlook – John D Jun 28 '22 at 02:09
  • Interesting observation. I launched MS Word and signed out of the account (I was signed in because I previously added the account to Outlook). After signing out, I tried adding the account programmatically, but it failed. I then manually added the account in Outlook. I entered the password when a prompt appeared and successfully added the account. I manually removed the account. And then, I was able to add back the account programmatically. The question still remains: why does it never fail when manually adding with Outlook, but programmatically it fails in certain circumstances – John D Jun 28 '22 at 14:49

0 Answers0