How to use C# to send Outlook 2013 emails RRS feed

  • Question

  • Hey, so I have a very simple thing that I am trying to do, and despite searching this website and Stack Overflow, I can't seem to find my answer.

    I'm just trying to write a Console App with in Visual Studio 2017 that I can use Task Scheduler to auto run once every two weeks and it will open Outlook 2013, prepare and send the exact same e-mail, with some text and a clickable hyperlink in it.

    I see a lot of mentions of Interop, but it looks like that is for Outlook 2010 and earlier, so it doesn't do me any good. I also see a lot of folks mentioning using Outlook.Application, but Visual Studio apparently doesn't know what that is without me adding some stuff to the top of my code in the "using" section. And I can't find what I need to add up there.  I found a website from Microsoft that says I have to enable the interop functionality at the time of install for Office 2013, and I hope that's not the case because I can't reinstall Office 2013 on this computer so there would be no way for me to do this.

    I know this is a dumb and simple question, but I seriously can't seem to find the answer I need anywhere.

    Tuesday, December 4, 2018 3:12 PM

All replies

  • Interop is not at all Outlook 2010 specific - it is the technology used to make .Net work with native COM. Add a reference in your project (from the COM tab) to the "Microsoft Outlook.." library, and VS IDE will generate an interop dll. Or you can add a reference to an existing interop dll in case you want to support an earlier version of the COM library or develop/compile on a machine where the COM library is not installed. Interop dll is nothing but a glorified binary header with no executable code, it just tells .Net what the COM library objects and methods are.

    The object you want to create is Outlook.Application (it is the only creatable OOM object).

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, December 4, 2018 3:24 PM
  • Thank you for the response!

    I'm still a bit lost, however. I added the COM reference "Microsoft Outlook 15.0 Object Library" and "Microsoft Outlook View Control" and Visual Studio is still telling me that "Outlook.Application exist in the current context". Do I need to add a using statement at the top? If so, what statement? I tried doing something like

    using Outlook = Microsoft.Foo;

    but there's nothing I can add after "Microsoft" that makes any sense. I was looking for something like "Office" or "Outlook", but it's not there. All that exists is "CSharp", "Sqlserver", "Visualbasic" and "Win32"

    EDIT: Well, I think I figured that part out. The part where I add the reference. Now I just need to figure out what to do with it! Google, here I come :)

    • Edited by TnD_Guy Tuesday, December 4, 2018 4:44 PM Think I got it.
    Tuesday, December 4, 2018 4:18 PM
  • Look at the name of the reference in the References node in your project.

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, December 4, 2018 6:22 PM
  • Hi TnD_Guy,

    You can refer to the following steps:

    1. Create a Console App and Add reference "Microsoft.Office.Interop.Outlook", like this:

    2. Select "Microsoft.Office.Interop.Outlook 15.0":

    3. Add reference to the code, you can copy all code as below and run it:

    Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)olApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                mailItem.To = "emailaddress";
                mailItem.Subject = DateTime.Now.ToString("yyyyMMdd") + "report";
                mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
                string content = "Time" + DateTime.Now.ToString("yyyyMMdd") + " Thanks!";
                content = "test,<br/> <br/>test:<br/> <br/>" + content + "<br/> <br/><br/><br/>test。";
                content = content + "<br/>\r\n                                    <br/>Best Regards!\r\n                                    <br/>\r\n                                    <br/>          \r\n                                    <br/>==============================================\r\n                               \r\n                                    <br/>\r\n                                    <br/>\r\n                \r\n             ===============================================";
                mailItem.HTMLBody = content;
               // mailItem.Attachments.Add(@"c:\test.rar");
                mailItem = null;
                olApp = null;

    Then code parts finished. At least, you need to create a Task program to run your code.



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, December 5, 2018 6:08 AM