How to use the Outlook Object Library to send an email


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.
    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.Subject = "Subject";


Happy Coding

Retrieving the COM class factory for component failed due to the following error: 800702e4.


While working on some outlook C# application I got a COM exception like this.

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 800702e4.

My code was simply straight forward, I was just creating the instance of the outlook application.

var oApp = new Outlook.Application();

And this code was used in a different application which works without any issue. Later I found the solution for the problem. The culprit was UAC. I was running the Visual Studio as Administrator. So the conclusion is, when automating an Office application from another application, both the application should be run in same privilege level. If you are running Visual Studio as Administrator, you should run the office application also in Administrator mode.

Happy Programming.

Covert PowerPoint Slides to Images


Today one of my colleague comes with a problem; he want to display a Powerpoint presentation in his Sharepoint site. But we didn’t got any direct way to display it. They we tweaked the code, to export the Slides to images and display it using the a custom slideshow web part, like the AjaxToolkit slideshow control.

I am attaching the code to export all the slides from a Power Point to Images

string ExportLocation = "C:\\Sample";
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
ppApp.Visible = MsoTriState.msoTrue;
ppApp.WindowState = PpWindowState.ppWindowMinimized;
Microsoft.Office.Interop.PowerPoint.Presentations oPresSet = ppApp.Presentations;
Microsoft.Office.Interop.PowerPoint._Presentation oPres = oPresSet.Open("C:\\ScreenShots.ppt",
            MsoTriState.msoFalse, MsoTriState.msoFalse,
ppApp.ShowWindowsInTaskbar = MsoTriState.msoFalse;	//Hiding the application; But it will be displayed always
    Slides objSlides = oPres.Slides;	//Getting all the slides
    for (int i = 1; i < objSlides.Count - 1; i++)
        string file = Path.Combine(ExportLocation, string.Format("{0}.{1}", objSlides[i].Name, "jpg"));
        oPres.Slides[i].Export(file, "jpg", 800, 600);
    ppApp.Quit();	//Closing the Powerpoint application. Sometimes it won't work too.

Thanks to ArunKumar, for providing the code snippet.