Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > How can I change credentials to read a particular mailbox?
Ask a questionAsk a question
 

QuestionHow can I change credentials to read a particular mailbox?

  • Friday, June 26, 2009 4:00 PMLucky7777 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have a simple code to get all emails from exchange server using outlook lib.

    Doesn’t mater what parameters I set for name , pass , it reads only my email mailbox

    (where name is a Profile Name)

     

    How can I change credentials to read a particular mailbox?

     

     

    Outlook.Application outlook = new Outlook.ApplicationClass ();

    Outlook.NameSpace ns = outlook.GetNamespace("Mapi" );

     

    ns.Logon(name , pass , true , true );

    Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders .olFolderInbox);

All Replies

  • Monday, June 29, 2009 5:29 PMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Until now you only can have one Excahnge Inbox per Profile.
    If you you want to connect to another Mailbox you need to create a temporary profile.

    Another way is to open a shared folder if you have the access rights to the other inbox.

    Something like this:
    CalendarFolder =
    ns.GetSharedDefaultFolder(myRecipient,Outlook.OlDefaultFolders .olFolderInbox);         
      
    http://msdn.microsoft.com/en-us/library/aa220116(office.11).aspx

    Hope this helps,
    greets - Helmut


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Monday, June 29, 2009 5:42 PMLucky7777 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ok, thanks.

    I have 2 Accounts (so two profiles)

    trying "ns.Logon(A_ProfileName , pass , true , true ); " but it uses only default profile. (doesn't matter what profile name I use to Logon.
  • Monday, June 29, 2009 8:08 PMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is Outlook already running in the background when you are running this code?

    Greets - Helmut 
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Monday, June 29, 2009 9:23 PMLucky7777 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I tried both cases (on/off outlook)
  • Tuesday, June 30, 2009 6:37 AMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Have you checked if there is an Outlook process running in the background using Task-Manager?
    Sometimes other programms starting Outlook without a GUI.
    e.g. Sidebar Widgets or Active Sync / Mobile Device Center.

    Also make sure that you are using the correct Profilename.
    Try to log it.

    Must be working with two profiles.

    Greets - Helmut
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Tuesday, June 30, 2009 6:00 PMLucky7777 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have no outlook scheduled tasks.
    Checked "Windows Task Manager" - no outlook running.
    Double checked Profile names.

    ns.Logon() picks only default profile.
  • Tuesday, June 30, 2009 7:47 PMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Lucky,

    I can remember -  I programmed something using two different Outlook Profiles.
    And that worked.
    It was this article here:


    http://www.codeproject.com/KB/office/BridgingTheGap.aspx

    So - can you create a simple WinForms application with a combobox that has your 2 profile-names.
    Your code should look like this.


    // Create an Outlook session
    object missing = System.Reflection.Missing.Value;

    // get the Outlook Application Object
    Outlook.Application outlookApplication = new Outlook.Application();

    // get the namespace object
    Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

    // Logon to Session, here we use the name from
    // the comboBox to logon to a specific session.
    // If there is already an Outlook instance with
    // a Session - then this profile is used.
    olNamespace.Logon(comboBox1.Text ,  missing, true, true);
    try
    {

       // Only in Ol2007
       MessageBox.Show(olNameSpace.CurrentProfileName);

    }
    catch (System.Exception ex)
    {
       
        MessageBox.Show(this,ex.Message);
    }
    finally
    {
        fabric = null;
        olNamespace.Logoff();
        olNamespace = null;
        outlookApplication = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    Greets - Helmut
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Friday, July 03, 2009 4:51 PMLucky7777 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Lucky,

    I can remember -  I programmed something using two different Outlook Profiles.
    And that worked.
    It was this article here:


    // Logon to Session, here we use the name from
    // the comboBox to logon to a specific session.
    // If there is already an Outlook instance with
    // a Session - then this profile is used.
    olNamespace.Logon(comboBox1.Text ,  missing, true , true );
    Greets - Helmut
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Right, but outlook doesn't pick another profile. All the time it runs only default one.