none
Creating draft emails in another users profile using Microsoft.Office.Interop.Outlook: C# RRS feed

  • Question

  • I am trying to create draft emails in another profile while no other profile or instance of outlook.exe is running.  I pass in 

    a profile name (m_profileName) as a parameter.  It still only creates the draft emails for my profile, even though I specify a different profile.  I have made sure outlook.exe is not in task manager and that outlook is not running.  Is there a different way to accomplish what I am trying to do using the code below or can it not work this way.

     if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
                {
    
                    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                    application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                    Console.WriteLine("FOUND OUTLOOK PROCESS RUNNING");
                }
                else
                {
    
                    // If not, create a new instance of Outlook and log on to the default profile.
                    application = new Outlook.Application();
                    Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
    
                    try { 
                    nameSpace.Logon(m_profileName,Missing.Value, false, true);
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine("THIS FAILED TO LOGON TO PROFILE" + e.StackTrace.ToString());
                    }
                    //nameSpace.Logon.Outlook = "reporting";
                    //   nameSpace = null;
    
                    Console.WriteLine("CREATED NEW INSTANCE OF USER => " + m_profileName);

    Monday, March 21, 2016 9:35 PM

All replies

  • What is the name of the profile? Does it actually exist in Control Panel | Mail | Show Profiles?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, March 21, 2016 9:45 PM
  • The name of the variable that I pass in for the profile matches the name of the profile I have in Control Panel | Mail|Show profiles.  I currently have my personal profile to "Always use this profile".  Does that override what I set in my code?  When I select "Prompt for a profile to be used", a prompt shows up and lets me select.  Is there a way to suppress the prompt and then pass the profile to be used programmatically?
    Monday, March 21, 2016 9:50 PM
  • You can overwrite the registry key...

    Why are you trying to use a different profile? Wouldn't that produce different results based on whether Outlook is running or not?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, March 21, 2016 10:06 PM
  • Did you try to specify the password if any?

    Anyway, try to use the following line of code instead:

    nameSpace.Logon(m_profileName,string.Empty, false, true);

    or

    nameSpace.Logon(m_profileName,string.Empty, true, true);


    Do you see the dialog for choosing a profile?

    Note, you need to use an empty string if the profile doesn't have a password set. So, use string.Empty instead of Missing.Value.
    Tuesday, March 22, 2016 6:48 AM
  • I attempted to use the below.  The select a profile dialog box shows up.  I thought the false was supposed to suppress the dialog box from showing up?
    nameSpace.Logon(m_profileName,string.Empty, false, true);
    Tuesday, March 22, 2016 1:32 PM
  • Hello Double,

    And the following line of code:

    nameSpace.Logon(m_profileName,string.Empty, false, true);

    Does it work correctly?

    Tuesday, March 22, 2016 2:17 PM