Having problem in Microsoft Outlook Automation RRS feed

  • Question

  • User213957653 posted

    hi guys,

                 I am working on a windows service in which i'm trying to automate outlook to send emails  periodically based on some criteria.

    I developed and tested the code in a console application first and it worked fine. But when i migrated from console to windows service it started misbehaving. Its giving various exception as:

    1) Here is the exception that I get when running the service. Service cannot be started. System.Runtime.InteropServices.COMException (0x80020009): Cannot complete the operation. You are not connected. at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetDefaultFolder(OlDefaultFolders FolderType) at ClassLibrary.BaseService.SortInbox() in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 48 at ClassLibrary.BaseService.Update() in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 311 at ServiceAtBase.UpdateService.OnStart(String[] args) in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ServiceAtBase\ServiceAtBase\UpdateService.cs:line 24 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

    2)Microsoft Outlook has encountered an error and needs to close.

    3)outlookObj.AnserWizard threw an exception of type 'System.Runtime.InteropServices.COMException'

    Here is my code:

    bool isOutlookRunning = false;
                Process[] process = null;
                process = Process.GetProcessesByName("OUTLOOK");
                if (process.Length > 0)
                    isOutlookRunning = true;
                    obj2 = new Microsoft.Office.Interop.Outlook.Application();
                    if (isOutlookRunning)
                        foreach (Process pr in process)
                            if (pr.ProcessName.Contains("OUTLOOK"))
                    obj2 = new Microsoft.Office.Interop.Outlook.Application();
                ns = obj2.GetNamespace("MAPI");
                inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                outboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
                process = null;


    These errors are not fixed as well.

    At one time one exception will pop up and other time some other.

    Any help will appreciated...


    Friday, November 27, 2009 3:43 AM

All replies

  • User-1659704165 posted


    This code will work if Outlook is installed where Ur executing..


    U may Create window Service which Send mail using SMTP

    Friday, November 27, 2009 4:08 AM
  • User213957653 posted

    Thanks for the reply,

    Yes Outlook is installed where the service will be installed and my requirements are like that  I have to send mails through outlook.

    Thanks again

    Friday, November 27, 2009 4:27 AM
  • User-1659704165 posted


    Look below code..

    / Create a new MailItem.
      MailItem myMail =
      // Now gather input from user.
      Console.Write("Receiver Name: ");
      Console.Write("Subject: ");
      myMail.Subject = Console.ReadLine();
      Console.Write("Message Body: ");
      myMail.Body = Console.ReadLine();
      // Send it!



    Friday, November 27, 2009 4:34 AM
  • User213957653 posted

    Hi  qwe123kids,

                            I think u've misunderstood my problem.The prob is not in sending the email. As i've wrote here that the same code was working absolutely fine in a console application and prob arises only in windows service. This is not the full code dat i've posted its the part of the code that is causing the prob.

    I've tried using different accounts in my service but to no avail.I've tried both on windows vista and windows xp as well.



    Friday, November 27, 2009 4:40 AM
  • User-1659704165 posted

    seems U have not checked My link Rather thn calling Outlook process u may use office Automation It may help lot in any situation of Outlook..

    Chk the link

    Friday, November 27, 2009 5:00 AM
  • User213957653 posted

     hello qwe123kids,

                                     I've checked ur link(thanx 4 dat).But i've already used these things in my app.As i said earlier dis is not the full code. I m working on dis for quite some time now. and i've been googling a lot and found out dat the exception can be caused by if outlook is not up and running but i got the exceptions at both time even if its running or not so by using outlook process i'm making sure either its caused by a running outlook or the other way its sorted any ways. I've tried giving administrator account to the service as well as with the local system account 'allowed it to interact with the user' so its not a permission problem either.

    If this info can help.


    Friday, November 27, 2009 5:51 AM
  • User213957653 posted

    Sorry I forgot to mention one thing...

    when i give my service administrator account service does not start due to logon failure in windows vista but does on xp but with same above exceptions. and i'm giving the correct passord to it.



    Friday, November 27, 2009 7:24 AM
  • User213957653 posted

    Can any one be of any help???? 

    Saturday, November 28, 2009 1:57 AM