Reading email from Outlook using C#

clock March 20, 2009 17:04 by author Vladimir

I recently had to read and parse some emails that I got that were in an Outlook folder. I wrote a small program that did it. Here I’m sharing my experience.

In order to be able to use Outlook wrapper classes in your program, you must add references to the correct assembly. In Visual Studio in the Solution Explorer, right click on References and click Add Reference. Now you need to locate the correct library. For Outlook 2007, go to the COM tab and choose Microsoft Outlook 12.0 Object Library.

Now in your C# file, add the following using statement:

using Microsoft.Office.Interop.Outlook;


The following sample will write out the subjects of all the emails in your Inbox:

ApplicationClass outlook = new ApplicationClass();
Folder folder = outlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox) as Folder;
foreach (var item in fol.Items)
{
   MailItem mail = (MailItem)item;
   Console.WriteLine(mail.Subject);
}


You can use the Body property of MailItem to get the body of the message, etc.

Note: Outlook will let you read the subjects just fine, but if you attempt to look into the body of the message, it will present a security dialog that will tell you that an external application is trying to access data and if you would like to allow this action.

If you want to get a different folder with your own custom name and not one of the default folders, you can loop through all the folders in outlook.Session.Folders until you find the correct one:

foreach (var folObj in folder.Folders)
{
   Folder fol = (Folder)folObj;
   Console.WriteLine(fol.Name);
}


That’s all there is to it.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Free Book Giveaway

clock September 9, 2008 19:37 by author Vladimir

Dave Ward from encosia.com has three copies of Advanced ASP.NET AJAX Server Controls For .NET Framework 3.5 to give away, but there's a catch. In order to get a shot at getting one of the books, you must participate in an open source project that he has started on Codeplex.

The project is an ASP.NET AJAX Server control that will validate username availability on the server and show an inline confirmation as to the availability.

You can find the project here on Codeplex and get more information about the giveaway and ways to participate here on encosia.com.

BTW, if you're interested in ASP.NET AJAX development, subscribe to encosia.com via an RSS reader or visit the site sometimes, he's got great tips there.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Word PDF Converter

clock July 26, 2008 23:15 by author Vladimir

At my job, I sometimes have to convert a bunch of Word documents to PDF. As this is tedious to do manually, I have written a small utility that lets me automate the process. I'm now sharing it for free with the world.

This is a small utility that lets you choose a bunch of files and converts them to PDF.

Download Word PDF Converter 1.0

There are a few prerequisites to run the software:

  1. Word 2007
  2. Save as PDF plugin (free from Microsoft)
  3. .NET Framework (free from Microsoft) - at least version 2.0

 

I'm also providing the source code for the app, so you can see how it works, and use it as sample code in your own apps.

Download Word PDF Converter Source Code

Instructions on using the software:
Click on Add Files or Add Directory and choose the files that you want to convert. Add Files will let you choose any files that you want (as long as Word can open them, they should work), Add Directory will only add files with extensions of .DOC or .DOCX in the directory that you choose. Then just hit the convert button, and once it finishes you'll find the PDF files in the same directory as the original files.

I hope you find this little app handy and useful.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


TimeZones with C# on Windows Mobile

clock July 6, 2008 18:53 by author Vladimir

As I'm sure you know, the TimeZone support in the .NET Framework is abysmal to say the best. You can get the current time zone and also convert DateTime objects to UTC and that's pretty much it. Even with .NET Framework 3.5, Microsoft only fixed the problem on the desktop (by including the TimeZoneInfo class). The .NET Compact Framework still has no native TimeZone support. Here is an attempt to fix that.

I'm offering my TimeZoneInformation class free of charge for anyone that wants it.

The basis for this class is taken from here. William Stacy wrote this class for Windows. What it does is read the time zone information from the registry and then use regular (non .NET) functions to convert between them.
There is another version of this class found here. This one is written by Mike Dimmick.
I'm not sure who wrote it first, so I'm giving credit to both of them. Thanks guys!

There needed to be a bunch of changes made to that class in order for it to work on Windows Mobile.
First of all, the functions are located in a different DLL file in Windows Mobile than they do on Windows.
Also, there are no functions to convert to a generic time zone in Windows Mobile, only between local time zone and UTC. Due to this, my class has to set the local time zone to whatever time zone that you need for a split second to do the conversion. I hope that this will not cause any problems with 3rd party programs.

The biggest problem in Windows Mobile, however, is that time zone information is not stored anywhere in the registry. In fact, I have absolutely no idea where it is stored.
Because of this, I wrote a small utility that exports the time zone information from a Windows PC to an XML file. My class now takes all of the information from this XML file and not from the registry as William's class does on Windows.

Important: my classes require .NET Framework 3.5. You can get the version for Windows here and the .NET Compact Framework 3.5 here.
These also come with Visual Studio 2008. You can get the free Express edition of Visual Studio straight from Microsoft here.

Without further ado, here are the downloads. I hope you will find them useful.

Source:

Binary:

XML file:

This is an XML file that I exported using my exporter utility on Windows Vista. Download it here.

Sample program:

I have written a sample program using my class. All it does is display the current time in the local time zone, in UTC, and in any other selected time zone. This is meant to run on a Pocket PC. This was tested with the emulator running Windows Mobile 5 and my own Pocket PC running Windows Mobile 6.

  • Source code
  • Binary - make sure to copy the XML file to the root folder on your Pocket PC for it to work.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions. Any code on this site does not come with any warraties whatsoever. Use it at your own risk.

© Copyright Vladimir Burmistrovich 2008

Calendar

<<  July 2009  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Sign in