Dec 14, 2013 · 2 minute read
It has traditionally been quite painful to interact with any part of the Microsoft Office product family from a C# application, but thanks to the introduction of dynamics and optional parameters over recent years, the process has dramatically improved.
Before doing anything, it is important to note that you must have Microsoft Office 2013 installed for this to work. Seems obvious, but, its still worth mentioning. You also need two references;
The quickest way to add these references to your project is to right click on the References folder in your project, and click Add Reference. The Reference Manager dialog window will appear as shown below;
- Click the COM tab
- Type Outlook into the search box
- Tick Microsoft Outlook 15.0 Object Library
- Click OK
You should now see that the appropriate references have been added to your project;
Next, add the appropriate using directives to your code file.
using Microsoft.Office.Interop.Outlook; using OutlookApp = Microsoft.Office.Interop.Outlook.Application;
The second directive is a recommendation to avoid ambiguity with other classes with the name Application. In the constructor of your application (or wherever you want this code to go), create an instance of the Outlook Application and create a new
MailItem object, as shown;
OutlookApp outlookApp = new OutlookApp(); MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
Finally you can begin to flesh out your email.
mailItem.Subject = "This is the subject"; mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>"; //Set a high priority to the message mailItem.Importance = OlImportance.olImportanceHigh;
And to display the email, simply call the
There are literally dozens of things you can do to an Outlook Email, including adding attachments, business cards, images, recipient, CC/BCC fields.
To create an Outlook 2013 email from C#, simply add the Microsft Outlook 15.0 Object Library to your solution, add the appropriate using directives, create a new
Application object, and
MailItem object, and flesh out your email. When ready, simply call
MailItem.Display(false) to show the email to the user. Please leave a comment below if you found this post useful
About the author
I am Jon Preece, an experienced website and software developer from the United Kingdom, based in Manchester.
Throughout my 10+ year professional career I have worked in many sectors, including; e-commerce, financial services, marketing, healthcare, travel and accountancy. Get in touch via Twitter.