How to use the Outlook Object Library to send an email

Posted by Anuraj on Wednesday, August 28, 2013

.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



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe


Copyright © 2017 - Anuraj P. 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. Hosted with ❤ by GitHub