问题
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?
回答1:
Outlook, just like any other Office app, cannot be used from a service. Your options are
In case of an Exchange mailbox, you can use EWS to connect to a particular mailbox
Extended MAPI (C++ or Delphi only) - you can dynamically create a profile, then add and configure the Exchange MAPI service (MSEMS).
Redemption (any language) - 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.
来源:https://stackoverflow.com/questions/26403680/access-ms-outlook-from-windows-service