title: MAPI_E_CALL_FAILED (0x80004005) when try to access MAPI via windows service
body: <p>I've developed a windows service which is designed to communicate with a mvc web application via SignalR.
  In this specific case the user want's to send an email, with predefined text/subject and some attachements.
  The mvc application passes an identifier to the service, which in return calls the api with the identifier, to get the files/attachements.
  It stores them to a temporary directory for later use.</p>
  
  <p>If the download is completed, then the service will call MAPI to pass the mail object to the standard mail client.</p>
  
  <p>To be clear: it works fine, as designed, when it is not running as a service, but an console application (for example) instead.
  If I run it as a service, I always get a MAPI_E_CALL_FAILED (0x80004005), no matter what I do.</p>
  
  <pre><code>public static extern HRESULT MAPILogon(IntPtr hwnd, string prf, string pw, int flg, int rsv, ref IntPtr sess);
  </code></pre>
  
  <p>in my application i then call:</p>
  
  <pre><code>var status = SimpleMAPIWrapper.MAPILogon(connection.Hwnd, connection.Profile, connection.Password, connection.Flags, 0, ref zero);
  </code></pre>
  
  <p>after that status contains the error code/message: <strong>MAPI_E_CALL_FAILED</strong> </p>
  
  <p>If I run it inside a console application, I always get <strong>S_OK</strong> (0x00000000), which is what I expected.</p>
  
  <p>First I thought it may be that no mail client was associated when running as system/network service. But it always fails, when running the service as the same user as when running it as console application.</p>
  
  <p>Any idea, what I am missing?</p>
  
  <p>EDIT: This solution should not just work with outlook, but any third party mail client.</p>