MS Outlook Integration with C# - Reading Mails


Prerequisite

  • Microsoft Office Outlook 2007 installed and set up with an Outlook profile.
  • Visual Studio 2008, C# language.

Note: Like other MS Office products, Outlook also must be running as a background process during automation. 

Without opening Outlook we cannot read or access the mail boxes (inbox, sent...), contacts etc.

References used:

Microsoft.Office.Interop.Outlook

Untitled.png

Outlook Active Profile

To set up an active profile do as shown in the following images from the Control Panel, then Mail setup.

1.png   2.png 

This will already be created when we configure an outlook account. If there were multiple accounts configured on 

a single system, then set one account as an active default profile.

Problem Faced during Project cycle

The project was a mail automation tool which needs to process and extract mails which haves an attachment (only .pdf) from the Outlook Inbox on a daily basis. It's a kind of scheduler which schedules for every 5 hours, fetches records and uploads them to the database. On a daily basis more than 1500 mails are saved in the Inbox and also no old mail is deleted. 

Initially the program does a for-loop for all the mails for reading the attachments. It takes almost half an hour to complete the process. 

Also, some mails having .zip file contains .pdf files as attachments. So I need to capture those mails and extract .zip files also. So the tool reads all the mail even though there are no attachments. I tried to optimize the code by refereeing a couple of ways but in vain.

I am sure there are better ways to optimize the program.

Getting Started

Now the challenge is started.

  • How to find those mails which having attachments.
  • How to find those mails which come on the same day.
  • How to find those mails which have a specific subject line etc...

I searched many articles for a specific logic to find the correct output. And coincidently (luckily :-)) came across this MSDN article, 

which solves all the problems. 

urn:content-classes:message

Reference from MSDN:

The urn:content-classes:message content class defines a set of properties for an item that is a message.

http://msdn.microsoft.com/en-us/library/aa123730(v=exchg.65).aspx

Using this we can create SQL-like statements or expressions which filter out or return those mails that satisfy the conditions.

For e.g.:

String sFilter = "@SQL=" + "urn:schemas:httpmail:subject LIKE '%c-sharpcorner%' AND "

 + "urn:schemas:httpmail:datereceived >= '" +                                    

    (DateTime.Now.ToUniversalTime()).ToString("MM/dd/yyyy") + "' AND "

 + "urn:schemas:httpmail:hasattachment = True";

 

This filter will return those mails which contain the subject c-sharpcorner and contains attachments of today's date.

Code Explained

For more details about Microsoft.Office.Interop.Outlook reference: http://msdn.microsoft.com/en-us/library/ff458122.aspx

  • Creating an Outlook ApplicationClass class.
  • Creating an Outlook _NameSpace method - returns a NameSpace object of the specified type.
  • Calling the Logon method - Logs the user on to MAPI, obtaining a MAPI session.
          For more details refer to: http://msdn.microsoft.com/en-us/library/ff861594.aspx
  • After successful logon, the Outlook.exe will get opened in background. You can view it  from Task Manager.
  • Refreshing the AppFolders (Inbox, Sent Items... etc).
  • Filtering mails on condition from inbox, above sample code.
  • Retrieving mails and saving the attachments.
  • If any .zip attachments found, using an open source reference the .pdf file get extracted.
  • The open source is Iconic.Zip: http://dotnetzip.codeplex.com/.
  • After all the process completed, the entire instance got disposed.

Error
 

3.png
 

One common error can occur during the process shown above. The solution is to reconfigure the email account by providing 

the correct user credentials and mailing server credentials.


Conclusion

 

A sample project is attached for reference, post your comments and rate the article also !



Up Next
    Ebook Download
    View all
    Learn
    View all