1

I'm writing a console application, which checks the contents of an outlook mailbox, in order to read the contents of specific emails into a database.

This application works fine within Visual studio, whether or not Outlook is open.

If I build the application and run it from the exe it only works when Outlook is open, which isn't really a problem.

However, I need to run it from a scheduled task as it has to run every few minutes. This will not work at all.

I'm using the following code:

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");

int collCount = processes.Length;

if (collCount != 0)
{
    OutlookApp = Marshal.GetActiveObject("Outlook.Application") as Application;  
}
else
{
    OutlookApp = new Application();
} 

The error message I'm getting is:

System.Runtime.InteropServices.COMException (0x800401E3): Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk) at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID) at ImportCruiseEmails.Program.Main()

On the line :

Marshal.GetActiveObject("Outlook.Application") as Application;

Both Outlook and the console application are running under my user account, which has administrator permissions. I've been pulling my hair out with this all afternoon. Can anybody please shed any light on it? Cheers!

Ebad Masood
  • 2,389
  • 28
  • 46
fourdam
  • 116
  • 1
  • 12
  • 1
    Are you absolutely sure that your program and Outlook are running under the same user? – xxbbcc Nov 14 '14 at 19:41
  • Well the Outlook process is running under the same user as I have set up the scheduled task with, however according to Dmitry's answer below this is not the same. – fourdam Nov 14 '14 at 21:57

1 Answers1

6

Even through the user accounts are the same, security contexts are different since the scheduler runs as a service. And no Office app can be used in a service.

Your options are

  1. In case of an Exchange Server, use EWS to access the mailbox.

  2. Extended MAPI (C++ or Delphi only)

  3. Redemption (I am its author, any language) - it wraps Extended MAPI and its RDO family of objects can be used from a service.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks for your response. Although it's currently being used on a POP3 account, I could route the emails to an account on our Exchange server in order to use EWS. Stupidly when I started creating this I didn't even consider that I would actually need to have Outlook open all the time in order for the new emails to actually come into the inbox, which is by no means ideal. I would assume then that if I was to use EWS this would no longer be an issue as it would communicate directly with the Exchange server? – fourdam Nov 14 '14 at 22:03
  • My IT dept wouldn't allow EWS, so I had a look at Redemption. Whilst I managed to get it to work, using it to access the SafeMailItem object, I could still only do this when I manually ran the application. I did a lot of searching and I couldn't find any answers on how to use it to bypass the permissions issues I originally had accessing Outlook in the first place, from a scheduled task. In the end I just stuck a timer in the app and ran it manually, which removed the need for a scheduled task and solved the permissions issues, but it would be good to know how to do it with Redemption? – fourdam Nov 18 '14 at 14:06
  • Do not use SafeMailItem - Safe*Item objects are designed to be used alongside your existing Outlook Object Model code with minimal modifications. Use the RDO family of objects - RDOSession and its child objects are a replacement for CDO 1.21 or OOM (but can still be used alongside OOM if you want). See http://www.dimastr.com/redemption/rdo_introduction.htm – Dmitry Streblechenko Nov 18 '14 at 17:47