Microsoft Developer Network > Domovská stránka fór > Visual Studio Tools for Office > How can I change credentials to read a particular mailbox?
Odeslat dotazOdeslat dotaz
 

DotazHow can I change credentials to read a particular mailbox?

  • 26. června 2009 16:00Lucky7777 Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     

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

Všechny reakce

  • 29. června 2009 17:29Helmut ObertannerMVP, PřispěvatelUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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]
  • 29. června 2009 17:42Lucky7777 Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.
  • 29. června 2009 20:08Helmut ObertannerMVP, PřispěvatelUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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]
  • 29. června 2009 21:23Lucky7777 Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    I tried both cases (on/off outlook)
  • 30. června 2009 6:37Helmut ObertannerMVP, PřispěvatelUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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]
  • 30. června 2009 18:00Lucky7777 Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    I have no outlook scheduled tasks.
    Checked "Windows Task Manager" - no outlook running.
    Double checked Profile names.

    ns.Logon() picks only default profile.
  • 30. června 2009 19:47Helmut ObertannerMVP, PřispěvatelUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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]
  • 3. července 2009 16:51Lucky7777 Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.