0

I have created an application that accesses the Exchange Server through MAPI. A Tray app is used to start, stop and configure the application. I have put all of the communication and processing logic into a separate library which processes mail on a timer. When running in Debug mode, the library is accessed directly from the Tray app, but when running in release mode, it is being called by a service installed at the User level. See the diagram below.

enter image description here

I have everything configured to run using an x64 bit version of Outlook 2010. The issue I am having is that when I build and install in Debug (access the library directly), everything works properly. If I install the Release build and run as a service, the MAPI connection is not initializing.

I seem to be accessing the COM object, since I am getting a return value from the method I call rather than an exception. I have set the User account to my own login for the time, so I should have permission to access the profile. Although I could post the initialization code, but I don't believe it will help. I am thinking the issue has more to do with permissions. I have tried running a test install under each of the 4 account types associated with a service, and none of them work. Is it possible that my user account does not have access to my mail profile?

If anyone has any ideas on what I may be doing wrong, I would appreciate the insight.

EDIT

I am getting the HRESULT Cannot change thread mode after it is set. I was setting thread mode to false initially, but tried changing it to see if it would resolve the issue. Unfortunately it did not. I've added a code snippet below in case someone might find it helpful.

DWORD dwFlags=0;
if(bMultiThreadedNotifications) dwFlags|=MAPI_MULTITHREAD_NOTIFICATIONS;
if(bInitAsService) dwFlags|=MAPI_NT_SERVICE;

if(dwFlags) 
{
    MAPIINIT_0 MAPIInit={ MAPI_INIT_VERSION, dwFlags };
    lastErrorCode = MAPIInitialize(&MAPIInit);
    //if(lastErrorCode!=S_OK) return FALSE;
    if(FAILED(lastErrorCode)) result = FALSE;
} 
else 
{
    lastErrorCode = MAPIInitialize(NULL);
    //if(lastErrorCode!=S_OK) return FALSE;
    if(FAILED(lastErrorCode)) result = FALSE;
}
Tim
  • 2,731
  • 9
  • 35
  • 72
  • Any info in event log? – I4V Mar 27 '13 at 21:32
  • Most probably i think its Permission issue. can you try changing your windows service credentials(Default LocalSystem) with your windows user credentials(Logged in user) and see if it works... – sumeet kumar Mar 27 '13 at 21:32
  • @sumeet-kumar: Thank you for the response. I am running under the User account, although I have tried changing to all of the accounts to see if it would make a difference. I agree that it is probably a Permission issue, but not sure why the service does not have permissions under the User account. – Tim Mar 27 '13 at 21:41
  • possible duplicate of [Using MAPI to access the Exchange Server from a Service](http://stackoverflow.com/questions/14881631/using-mapi-to-access-the-exchange-server-from-a-service) – Ken White Mar 27 '13 at 22:27
  • @14V: Unfortunately, there are not any event logs outside of one that says the service started correctly, and the ones that I put there, – Tim Mar 27 '13 at 23:30
  • As per other post the Make sure the service runs under the identity of the mailbox owner!!! see if this helps – sumeet kumar Mar 27 '13 at 23:39
  • Can you be more specific? What is the relevant snippet of your code that fails? What are the errors? – Dmitry Streblechenko Mar 28 '13 at 01:42
  • how do you setup your timer? – Zdeslav Vojkovic Mar 28 '13 at 08:56

1 Answers1

0

The solution was posted by Dmitry Streblechenko, and can be found here: http://social.msdn.microsoft.com/Forums/en-US/outlookdev/thread/7a9cc40a-ffd6-4f83-9973-5410615b4df4.

Basically, MAPI was being initialized twice, so I had to add MAPI_NO_COINIT to my flags. After that, everything worked.

Thank you all for your help.

Tim
  • 2,731
  • 9
  • 35
  • 72