How to use the Outlook Object Library to send an email

August 28, 2013 by Anuraj

.Net Office Interoperability Windows Forms

Sometimes we required to send email messages using Outlook Object Library instead of the conventional System.Net.Mail.SmtpClient class. While developing an desktop application, where I couldn’t hard code the SMTP server details and credentials. I found a solution using Outlook Object library, which will help to send email, with the current user context and credentials.

Here is the code snippet.

var application = new Outlook.Application();
var mailItem =
    application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
//Adding attachments.
mailItem.Attachments.
    Add(@"C:\SampleAttachment.txt", DisplayName: "SampleAttachment");
//If email body contains some HTML formatting use this property.
//Instead use the Body property
mailItem.HTMLBody = "Hello World";
mailItem.Recipients.Add("someone@myserver.com");
mailItem.Subject = "Subject";

((Outlook.MailItem)mailItem).Send();

Happy Coding

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub