C# .NET - auto read emails (microsoft outlook)

Asked By susan susan on 06-Jan-07 12:42 AM


   Is there a way to write a program that will read a particular email (search by title and date sent/date recieved) from Microsoft Outlook? And it will auto extract the attached document and save it to a location on the local harddisk.

   Assuming that the Microsoft Outlook willl always be turned on and recieving emails every minute. And this program (compiled into a .exe) will be scheduled by the Task Scheduler to run daily a scheduled time.


using Microsoft.Office.Interop.Outlook - sundar k replied to susan susan on 06-Jan-07 01:02 AM

Accessing Outlook

The first requirement is to be able to access Outlook.  For those with Outlook/Office 2003, you should run the office install and choose .NET Programmability Support. For Office XP, you can download the Interop assemblies from http://support.microsoft.com/default.aspx/kb/328912?.

The appropriate assemblies will then be installed in the GAC, and you can then add a reference from your Visual Studio project to Microsoft.Office.Interop.Outlook.dll.

Add an appropriate using clause:

using Outlook = Microsoft.Office.Interop.Outlook;
Then you should be able to instantiate an Outlook object and make requests of it:

Outlook.Application app = new Outlook.ApplicationClass();
Outlook.NameSpace NS = app.GetNamespace("MAPI");
Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
This will give you access to inboxFld, which will allow you to iterate through the contents of the inbox! You can also change this to iterate through notes, or through calendar entries, tasks, etc. as you want.

For example, to iterate through your mail you can do:

foreach(Outlook.MailItem t in inboxFld.Items)

Now you can search for your particular mail using t.Subject and

    foreach(Outlook.Attachment Atmt in t.Attachments){
       'save the attachment to some filename, just for ex., c:\test.doc
        Atmt.SaveAsFile "C:\test.doc"

There's a sample VB code in one of our previous post, it can also help you out in using the same in your c# code.


How to display emails with attach iamges using c#.net - pradeep wasule replied to susan susan on 17-Oct-08 03:54 PM

end of post