0
votes

While trying to open Outlook for attachment in mail, via IIS asp.net website got this error. Assign IIS USR and Network with full permissions in DCOMCnfg to Microsoft OutLook component but nothing work.

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)).

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

// Create the Outlook application.
                Outlook.Application oApp = new Outlook.Application();
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                // Set HTMLBody. 
                //add the body of the email
                oMsg.HTMLBody = "Hello, This is test for sending pdf attachment using OutLook";
                //Add an attachment.
                String sDisplayName = "MyAttachment";
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                //now attached the file
                Outlook.Attachment oAttach = oMsg.Attachments.Add(Server.MapPath("~/TestSendFile.pdf"), iAttachType, iPosition, sDisplayName);
                //Subject line
                oMsg.Subject = "Your Subject will go here.";
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("user1@comecompany.com");
                oRecip.Resolve();
                // Send.

                oMsg.Display();
1

1 Answers

2
votes

Outlook Object Model (just like any other Office app) cannot be used from a service (such as IIS). More than that, you are attempting to display a message on the server side where nobody will ever see it.

You can use either

  1. Exchange Web Services (in case of an Exchange mailbox)

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

  3. Redemption - it wraps Extended MAPI and its RDO family of objects can be used from a service. It can be used from any language, including C#.

If you are attempting to display a message on the client side, your choices are

  1. mailto url - does not allow HTML or attachments
  2. Use Outlook Object Model from the client side Java Script. Your site must be trusted and you can ony use COM in IE.
  3. Generate an EML (MIME) file and provide a link to it - Outlook will be happy to open from the browser and display it.