Le réseau pour les développeurs > Forums - Accueil > Visual Studio Tools for Office > How can I change credentials to read a particular mailbox?
Poser une questionPoser une question
 

QuestionHow can I change credentials to read a particular mailbox?

  • vendredi 26 juin 2009 16:00Lucky7777 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    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);

Toutes les réponses

  • lundi 29 juin 2009 17:29Helmut ObertannerMVP, Auteur de réponseMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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]
  • lundi 29 juin 2009 17:42Lucky7777 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.
  • lundi 29 juin 2009 20:08Helmut ObertannerMVP, Auteur de réponseMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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]
  • lundi 29 juin 2009 21:23Lucky7777 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I tried both cases (on/off outlook)
  • mardi 30 juin 2009 06:37Helmut ObertannerMVP, Auteur de réponseMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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]
  • mardi 30 juin 2009 18:00Lucky7777 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I have no outlook scheduled tasks.
    Checked "Windows Task Manager" - no outlook running.
    Double checked Profile names.

    ns.Logon() picks only default profile.
  • mardi 30 juin 2009 19:47Helmut ObertannerMVP, Auteur de réponseMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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]
  • vendredi 3 juillet 2009 16:51Lucky7777 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.