views:

66

answers:

2

I'm trying to user Redemption to update a user's Outlook contacts. The user I'm affecting is passed in the exchangeUser, call him "Target User". This code works when I run it logged in as myself:

  public OutlookFolders(string outlookRootFolder, string exchangeUser, 
string mailServer)
        {
  var session = new RDOSessionClass();
  session.LogonExchangeMailbox(exchangeUser, mailServer);
  session.Stores.FindExchangePublicFoldersStore();
  var store = session.GetSharedMailbox(exchangeUser);
  //...
  }

I tried to log in as a 3rd user "Test User" who is not me and is not "Target User". My program brings up a password prompt at runtime when it gets to FindExchangePublicFoldersStore, and if I don't fill in my credentials it fails with the error:

         System.Runtime.InteropServices.COMException (0x8004011D): Error in 
         IMAPISession.OpenMsgStore(pbExchangeProviderPrimaryUserGuid):
         MAPI_E_FAILONEPROVIDER
            ulVersion: 0
            Error: Microsoft Exchange is not available.  Either there are network
            problems or the Exchange computer is down for maintenance.
            Component: Microsoft Exchange Information Store
            ulLowLevelError: 2147746069
            ulContext: 1318

I tried giving "Test User" owner permission on "Target User's" Mailbox and Contacts folder. Doesn't seem to make a difference. What other permissions need to be set for this to work?

+1  A: 

The rule of thumb is to run your code as a user who can access the mailboxes in question, call LogonExchangeMailbox for the current user, then open other users' mailboxes using GetSharedMailbox.

Dmitry Streblechenko
Thanks for your help! Redemption is a great tool.
RossFabricant
A: 

Here's the code for Dmitry's answer. It also uses a function from Milan's blog.

        public OutlookFolders(string exchangeUser, string mailServer)
        {
            var session = new RDOSessionClass();
            var userFullName = GetFullName("DOMAIN-NT\\" + Environment.UserName);
            session.LogonExchangeMailbox(userFullName, mailServer);
            session.Stores.FindExchangePublicFoldersStore();
            var store = session.GetSharedMailbox(exchangeUser);
            rootFolder = store.GetDefaultFolder((rdoDefaultFolders)OlDefaultFolders.olFolderContacts);
        }

        public static string GetFullName(string strLogin)
        {
            string str = "";
            string strDomain;
            string strName;

            // Parse the string to check if domain name is present.
            int idx = strLogin.IndexOf('\\');
            if (idx == -1)
            {
                idx = strLogin.IndexOf('@');
            }

            if (idx != -1)
            {
                strDomain = strLogin.Substring(0, idx);
                strName = strLogin.Substring(idx + 1);
            }
            else
            {
                strDomain = Environment.MachineName;
                strName = strLogin;
            }

            DirectoryEntry obDirEntry = null;
            try
            {
                obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
                PropertyCollection coll = obDirEntry.Properties;
                object obVal = coll["FullName"].Value;
                str = obVal.ToString();
            }
            catch (System.Exception ex)
            {
                str = ex.Message;
            }
            return str;
        }
RossFabricant