Programmatically generate Outlook email

0

I am creating a button in asp.net c# that when clicked will open up Outlook window.

I am referencing to the Microsoft.Office.Interop.Outlook dll, and using this in using statement:

using Outlook = Microsoft.Office.Interop.Outlook;

This the code.

        private void CreateMailItem()            
        {
            try

            {
                var outlookApp = new Outlook.Application();

                var mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                //var mailItem = (Outlook.MailItem)
                //    Application.CreateItem(Outlook.OlItemType.olMailItem);
                mailItem.Subject = "This is the subject";
                mailItem.To = "someone@example.com";
                mailItem.Body = "This is the message.";
                mailItem.Importance = Outlook.OlImportance.olImportanceLow;
                mailItem.Display(false);
            }
            catch (Exception)
            {

                throw;
            }           
        }

I get error on the very first line, var outlookApp = new Outlook.Application(); The exception says:

{"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))."}

c#
asp.net
email
outlook
ms-office
asked on Stack Overflow Aug 21, 2015 by Ilyas • edited Dec 11, 2018 by Cœur

4 Answers

0

The exception you've posted is thrown when a referenced dll or depencies of this dll are not correctly installed.

In this case, it seems outlook or office is not with the correct version which you referenced, on your test machine?

answered on Stack Overflow Aug 21, 2015 by Ryfang
0

Can't post this as comment.

I would like to know why you prefer the use of Outlook Interop? I am using the mailto:// protocol if I wanted my program to send email on the user's current email client, though I use this on WinForms.

like http://www.rapidtables.com/web/html/mailto.htm

answered on Stack Overflow Aug 21, 2015 by user1251683
0

Outlook, just like any Office app, cannot be used from a service (such as IIS). Even if you did make it work, the new message window will be displayed on the server where the user will not see it anyway.

You can try to run a client-side JavaScritp code, but then you'd be limited IE only, Outlook would need to be locally installed, and your site must be trusted to be able to create COM objects in a script.

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.

Consider using System.Net.Mail namespace for creating and sending emails in ASP.NET.

answered on Stack Overflow Aug 22, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0