Can't get Outlook.Application interop work in my MVC 4 project

1

I've been trying to use this interop in my MVC 4 project. I tried to make it simple just to get the idea. but I get an error "Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80010001 Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))."

Here's my simple code:

    using Outlook = Microsoft.Office.Interop.Outlook;
    [HttpPost]        
    public ActionResult SendEmail(SendEmailModel model)
    {
        if (ModelState.IsValid)
        {
            Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace ns = app.GetNamespace("MAPI");
            ns.Logon("", "", Missing.Value, Missing.Value);


            Outlook.MailItem mailItem = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);

            mailItem.To = model.To;
            mailItem.Subject = model.Subject;
            mailItem.Body = model.Message;

            ((Outlook.MailItem)mailItem).Send();

            app = null;
            ns = null;

            return RedirectToAction("Index", "TechFile");
        }
        return View(model);
    }

Anyway I found Outlook redemption is an alternative way of doing this. but I don't know to get it started.

model-view-controller
outlook-addin
asked on Stack Overflow Feb 21, 2015 by Lester Echavez

2 Answers

1

I resolved this by following this. http://support.microsoft.com/kb/329854

answered on Stack Overflow Feb 21, 2015 by Lester Echavez
0

Also I'd recommend reading the Considerations for server-side Automation of Office article which states the following:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

answered on Stack Overflow Feb 21, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0