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("email@example.com"); mailItem.Subject = "Subject"; ((Outlook.MailItem)mailItem).Send();