0
votes

I am building an ASP webforms application in C# and I am trying to launch Outlook to send an email from the client computer.

I am using the following example, which I found online.

    public void sendEmail(object sender, EventArgs e)

{

        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();

        // Get the MAPI namespace.
        Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        // Log on by using the default profile or existing session (no dialog box).
        oNS.Logon(Missing.Value, Missing.Value, false, true);

        // Create a new mail item.
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        // Set HTMLBody. 
        oMsg.HTMLBody = "Test";

        //Subject line
        oMsg.Subject = "Test Subject";

        // Add a recipient.
        Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

        // Change the recipient in the next line if necessary.
        Outlook.Recipient oRecip;

        oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
        oRecip.Resolve();

        // Send.
        oMsg.Send();

        //Log off.
        oNS.Logoff();
    }

I am having issues at this line of code here:

oNS.Logon(Missing.Value, Missing.Value, false, true);

Even though I have an Outlook profile configured, when it runs this line of code, my app launches Outlook and displays the "Welcome to Outlook 2016" dialogue.

The expected behaviour is for it to launch Outlook, using the existing profile.

If Outlook is already open, I get an error stating:

System.Runtime.InteropServices.COMException: 'Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).'

The expected behaviour is if Outlook is already open, is for my app to use the existing Outlook process.

Any help greatly appreciated.

1

1 Answers

1
votes

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 possible workaround, you may consider a low-level API on which Outlook is based on - Extended MAPI. Or just any wrapper around that API such as Redemption.

If you deal only with Exchange profiles you may consider using Exchange Web Services (EWS), see Start using web services in Exchange.