简体   繁体   中英

Automate Sending an email through Microsoft Outlook

I am new to C# and I am attempting to automate sending an email from Outlook through the following code and it works fine in the development environment. I would like it to use the default user as the sender even if outlook is not open.

        private void EmailMessage(string recipient, string subject, string body)
    {
        Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.MailItem email = (Outlook.MailItem)application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

        try
        {
            email.Subject = subject;
            email.Body = body;
            email.To = recipient;
            ((Outlook._MailItem)email).Send();
            _emailConfirmation = true;
        }

        catch (System.Runtime.InteropServices.COMException ex)
        {
            Logging.LogError("Trip Email Failed", ExceptionHelper.GetInnerMostException(ex));
            _emailConfirmation = false;
        }

        finally
        {
            //release the objects used to send email after message has been sent\\
            if (email != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(email);
            if (application != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
        }
    }

All users are assigned an account and have Outlook installed with a valid anti-virus. My concern is when it goes live, it will fail on the creation of a new instance of outlook or something else I am just not seeing. Do you think this will work with what I intend to accomplish when it goes live?

There is no error in the code, however I am seeing a lot of posts from people saying that you should not create an instance of outlook.application directly.

https://msdn.microsoft.com/en-us/library/office/bb622502.aspx

I think I may just be paranoid because I've never used a PIA before

There can be two main reasons why your code fails:

  1. You get a security issue. See Outlook "Object Model Guard" Security Issues for Developers for more information about the issue and possible ways to bridge the gap and suppress or avoid such issues. Be aware, in some cases the dialog window will not be shown to a user, you just got an exception in the code.

  2. The Click2Run edition of Office 2010 doesn't support automation. See Office 2010 Click-to-Run compatibility with add-ins for more information. Also you may find the How to: Verify Whether Outlook Is a Click-to-Run Application on a Computer article.

Try to use the Recipients property instead of the To field. And then use the Resolve or ResolveAll methods. See How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.

Also I'd recommend adding any logging mechanisms to the code. So, you can analyze the log files and understand what is going on under the hood. For example, consider using the log4net library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM