none
log on to outlook failed when execute exe on windows server 2012 RRS feed

  • Question

  • Hi there,

    My app is trying to read all emails in the folders. when I execute the code manually on non-server machine, it succeeded. however, It will fail when execute on windows server machine either manually or through task scheduler.

    Unhandled Exception: System.Runtime.InteropServices.COMException: The server is
    not available. Contact your administrator if this condition persists.
       at Microsoft.Office.Interop.Outlook.NameSpaceClass.Logon(Object Profile, Obje
    ct Password, Object ShowDialog, Object NewSession)
       at email.Program.Main(String[] args)

     Eventually I need to run it through task scheduler. any insight on how to get it to work? 

     below is my code snippet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Outlook;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    namespace email
    {
     class Program
     {
      static void Main(string[] args)
      {
       // truncate table
       string strConnection = "Initial Catalog=HardwareEmailFeedback;Server=winbi18;User ID=HWUser;Password=HWUser01#";
       truncateTable(strConnection);
       Microsoft.Office.Interop.Outlook.Application app = null;
       Microsoft.Office.Interop.Outlook._NameSpace ns = null;
       Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
       Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
       Microsoft.Office.Interop.Outlook.Store docStore = null;
       List<EmailRecord> emailRecords = new List<EmailRecord> { };
       try
       {
        app = new Microsoft.Office.Interop.Outlook.Application();
       }
       catch (System.Exception ex)
       {
        Console.WriteLine("tying to get outlook app");
        throw ex;
       }

       ns = app.GetNamespace("MAPI");
       try {
        ns.Logon("myaccoutnprofile", "mypassword", false, false); 
       }
       catch (System.Exception ex) 
       {

    Console.WriteLine("trying to log on "); throw ex;
      }

    }

    thanks!

    Monday, January 30, 2017 11:33 PM

Answers

  • Hi,

    Please visit NameSpace.Logon Method (Outlook)

    Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.

    To get Outlook instance and log on, please visit  How to: Get and Log On to an Instance of Outlook

    Regards,

    Celeste

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by guojun yu Thursday, February 2, 2017 11:36 PM
    Tuesday, January 31, 2017 6:13 AM
    Moderator
  • Hello,

    First of all, I have noticed the following statement:

    > through task scheduler

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    Also on systems with UAC (Vista, Windows Server 2008 and up) to be able to automate Office applications you need to run your application under the same security context. I.e. if Outlook is run with admin privileges (Run As Administrator) and your application is run without admin privileges you will not be able to connect both applications. Your application simply will not find the Outlook instance. Note, only one Outlook instance can be run at the same time.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by guojun yu Thursday, February 2, 2017 11:35 PM
    Tuesday, January 31, 2017 8:48 AM

All replies

  • My environment is windows server 2013, logged on to the machine as admin with the email account, outlook is already running when run the exe.
    Tuesday, January 31, 2017 5:34 AM
  • Hi,

    Please visit NameSpace.Logon Method (Outlook)

    Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.

    To get Outlook instance and log on, please visit  How to: Get and Log On to an Instance of Outlook

    Regards,

    Celeste

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by guojun yu Thursday, February 2, 2017 11:36 PM
    Tuesday, January 31, 2017 6:13 AM
    Moderator
  • Hello,

    First of all, I have noticed the following statement:

    > through task scheduler

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    Also on systems with UAC (Vista, Windows Server 2008 and up) to be able to automate Office applications you need to run your application under the same security context. I.e. if Outlook is run with admin privileges (Run As Administrator) and your application is run without admin privileges you will not be able to connect both applications. Your application simply will not find the Outlook instance. Note, only one Outlook instance can be run at the same time.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by guojun yu Thursday, February 2, 2017 11:35 PM
    Tuesday, January 31, 2017 8:48 AM
  • Thank you very much for your detailed answer! this is very helpful! 
    Thursday, February 2, 2017 11:36 PM
  • Thank you very much for your detailed answer! this is very helpful! 
    Thursday, February 2, 2017 11:36 PM