SIGN UP MEMBER LOGIN:    
ARTICLE

MS Outlook Integration with C# - Reading Mails

Posted by Suthish Nair Articles | Office Development May 10, 2011
Here you will see MS Outlook Integration with C# - Reading Mails.
Reader Level:
Download Files:
 

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 !



Login to add your contents and source code to this article
share this article :
post comment
 

Outlook integration with sharepoint 2010 tasks using C# Users can connect the sharepoint task list to their outlook..the issue is that all the task appears there including those which are not assigned to the particular user...this is confusing..further as the other tasks do appear user are able to manupulate them... How to work around these two issues..? Has anyone resolved this...

Posted by Saurabh Gupta Feb 22, 2012

Nice! It's a good start. Actually, writing articles put stamp on the topic you write about that you know that stuff. Even after a year, you need to do the same, you know you can do it and how to do it and where to find the code, if you forget it.

Posted by Mahesh Chand May 11, 2011

I'm still a forum guy, our champion Vulpes is there now, so i taken a break. Now am charging up. Also, am trying to learn how to write a good article.. :)

Posted by Suthish Nair May 11, 2011

Nice to see you now expanding your horizon from forums to articles :).

Posted by Mahesh Chand May 11, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor