2

I use VS2010, C#, ASP.NET to read outlook email. I've setup outlook express 6 with my gmail (IMAP), I get a strange exception at the first line, where my COM object is being created, here is my code:

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

try
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    ns.Logon("gmail_id", "gmail_pass", false, true);

    inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works
    Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
    Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

    for (int i = 1; i <= subFolder.Items.Count; i++)
    {
        item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
        Console.WriteLine("Item: {0}", i.ToString());
        Console.WriteLine("Subject: {0}", item.Subject);
        Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
        Console.WriteLine("Categories: {0}", item.Categories);
        Console.WriteLine("Body: {0}", item.Body);
        Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
    }
}
catch (System.Runtime.InteropServices.COMException ex)
{

}
finally
{
    ns = null;
    app = null;
    inboxFolder = null;
}

it is my exception:

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

this exception is create at the first line:

app = new Microsoft.Office.Interop.Outlook.Application();

I've used Microsoft.Office.Interop.Outlook.dll file as my interop reference, what is going wrong here? I can read my Gmail inbox in outlook express 6, but I have no luck in my ASP.NET web app

Xaruth
  • 4,034
  • 3
  • 19
  • 26
Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115

2 Answers2

2

Try configuring the appropriate COM Activation permissions for Office apps to run under ASP.NET.

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • thanks dear SilverNinja, I test it, but should this procedure be performed for each user? In fact I'm trying to write a WCF which can be accessed by my Silverlight application, it is going to be used for reading email, currently I read IMAP directly but outlook should be added also, where should I change these settings? on my server only? – Ali_dotNet Jun 14 '12 at 14:16
  • This would only be done on the server for whichever account is running the Application Pool. For IIS 6 it would be IIS_IUSRS, but for IIS 7 it needs to be configured in the Application Pool settings. – SliverNinja - MSFT Jun 14 '12 at 14:25
0

Firstly, you should not ever use any Office app (Outlook included) in a service (such as IIS). No exceptions, it is simply not supported. Secondly, if you are using the Outlook Object Model (not in a service), you need to have Outlook (the real one) intalled. OE has nothing in common witt Outlook 2010/2007/2003/etc., except for the word "Outlook" in the name.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78