Search code examples
c#serviceoutlookoutlook-2010

Access MS Outlook from Windows service


I try to access MS Outlook from service this way:

oApp = new Outlook.Application();
oNS = (Outlook._NameSpace)oApp.GetNamespace("MAPI");
oNS.Logon(Missing.Value, Missing.Value, false, true);
oFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

If run this code as Windows Application it is working properly. But if I try run in as service I get error

Server is not available

at the line

oNS.Logon(Missing.Value, Missing.Value, false, true);

What is the right way to access MS Outlook folder from service?


Solution

  • Outlook, just like any other Office app, cannot be used from a service. Your options are

    1. In case of an Exchange mailbox, you can use EWS to connect to a particular mailbox

    2. Extended MAPI (C++ or Delphi only) - you can dynamically create a profile, then add and configure the Exchange MAPI service (MSEMS).

    3. Redemption (any language - I am its author) - its RDO family of objects can be used in a service. You can use RDOSession.LogonExchangeMailbox / LogonHostedExchangeMailbox / LogonPstStore to make Redemption create a dynamic profile and log to it. You can also use RDOSession.Logon and specify the profile name, but then your service must run as the local user who has that profile instead of the service account.