0
votes

I am trying to Compose email from the server side. I have Microsoft.office.core and Microsoft.Office.Interop.outlook reference installed and am using this code below. This Code work great for new email(outlook) in my Local Machine. but when i try to publish my website to the Dev/Test server this code does not work and i get this error irrespective i have outlook installed in both the servers

System.UnauthorizedAccessException: 
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)).

How to solve it. Any help is good..Thanks in Advance

 Outlook.Application outlookApp = new Outlook.Application();
 Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
 mailItem.Subject = "Notice to Proceed MnDOT Contract Number: " + CntrctID;
 mailItem.To = lstCntrctContractorContact[0].CNTRCTR_PROJ_Email;
 mailItem.CC = CntrctContactInfo.MnDot_EMAIL;
 mailItem.HTMLBody = EmailBodyMsg.ToString();
 mailItem.Display(false);
1

1 Answers

0
votes

Outlook, just like any other Office app, cannot be used in a service (such as IIS). Even if you made it work, the email would be displayed on the server where no user will be able to see it and interact with.

You can either:

  1. Use mailto url on the web page.

  2. in case of IE, you can use the Outlook Object Model in you client side script, but your site must be trusted to be able to do that.

  3. On the server side, you can use EWS to connect to Exchange Server if that is what the user uses. But you will need to know the user name and password.

  4. Create an EML file (it is text based) and let user open it - Outlook (locally) will be happy to display the message body (HTML included) and any images/attachments. To make sure Outlook open the EML fiel in the compose mode, include the "X-Unsent" MIME header with the value of 1.