Access users outlook email from IIS application

-1

We have hosted a .net application on our IIS server. This application tries to read the emails from the current logged in users outlook.

I am using the library using Microsoft.Office.Interop.Outlook; and below is my code.

I am able to view the emails when this code runs from my VS. The moment I deploy this application on IIS then I am unable to top read any emails.

This is the error which is logged.

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

Am i following the correct approach on accessing the emails or are there any different ways to archive this? please enlighten.

Below is the whole code.

try
        {

            outlookApplication = new Application();
            outlookNamespace = outlookApplication.GetNamespace("MAPI");
            inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            mailItems = inboxFolder.Items;

            foreach (object item in inboxFolder.Items)
            {

              if (item is Microsoft.Office.Interop.Outlook.MailItem)
                {
                        Microsoft.Office.Interop.Outlook.MailItem mailitem = (Microsoft.Office.Interop.Outlook.MailItem)item;
                        if(mailitem.ReceivedTime.Date ==DateTime.Today)
                        {
                            TempEmail objTempEmail = new TempEmail();
                            objTempEmail.From = mailitem.SenderEmailAddress;
                            objTempEmail.To = mailitem.To;
                            objTempEmail.CC = mailitem.CC;
                            objTempEmail.Subject = mailitem.Subject;
                            objTempEmail.Body = mailitem.Body;

                            lTempEmail.Add(objTempEmail);
                            Marshal.ReleaseComObject(mailitem);
                        }
                }

            }

        }
        catch (System.Exception ex)
        {
            log.Error(ex.Message + "" + ex.InnerException);
        }
        finally
        {
            ReleaseComObject(mailItems);
            ReleaseComObject(inboxFolder);
            ReleaseComObject(outlookNamespace);
            ReleaseComObject(outlookApplication);
        }
c#
.net
asp.net-mvc
iis
office-interop
asked on Stack Overflow Jul 19, 2018 by Hudhaifa Yoosuf • edited Jul 19, 2018 by Keyur Ramoliya

2 Answers

0

"Am i following the correct approach"

No. Outlook interop will only work on the local machine. All you will achieve is checking the AppPool's inbox, which won't exist.

answered on Stack Overflow Jul 19, 2018 by Davesoft
0

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. Read more about that in the Considerations for server-side Automation of Office article.

As a workaround you may consider using EWS or Outlook REST API if you deal with Exchange based mailboxes. See EWS Managed API, EWS, and web services in Exchange for more information.

answered on Stack Overflow Jul 19, 2018 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0