Read Outlook email by filter criteria


Outlook object model provides a more extensible object collections to send and read items. This blog speaks about how to read a email from outlook default profile and process the attachment. The Outlook Item object has a method called Find which allows to filter the mail based on subject, To, cc etc. The below code will get all the mail item with the specified subject. If the item has an attachment it will save it to the path specified.

To begin with Add a Outlook interop assembly to the reference in the project

image

Once added please copy paste the below code to run the sample

Code Snippet
  1. try
  2.             {
  3.                 Microsoft.Office.Interop.Outlook.Application MyApp = new Microsoft.Office.Interop.Outlook.Application();
  4.                 Microsoft.Office.Interop.Outlook.NameSpace MailNS = MyApp.GetNamespace("mapi");
  5.                 MailNS.Logon(MyApp.DefaultProfileName, Missing.Value, false, true);
  6.  
  7.                 Microsoft.Office.Interop.Outlook.MAPIFolder MyInbox = null;
  8.                 MyInbox = MailNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
  9.                 Microsoft.Office.Interop.Outlook._MailItem InboxMailItem = null;
  10.  
  11.                 Microsoft.Office.Interop.Outlook.Items oItems = MyInbox.Items;
  12.                 ////string Query = "[Subject] = '" + Properties.Settings.Default.EmailSubject + "'";
  13.  
  14.                 string Query = "[Subject] = 'my sample search text'";
  15.                 InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.Find(Query);
  16.                 while (InboxMailItem != null)
  17.                 {
  18.                     ListViewItem myItem = lvwMails.Items.Add(InboxMailItem.SenderName);
  19.                     myItem.SubItems.Add(InboxMailItem.Subject);
  20.                     string AttachmentNames = string.Empty;
  21.                     foreach (Microsoft.Office.Interop.Outlook.Attachment item in InboxMailItem.Attachments)
  22.                     {
  23.                         AttachmentNames += item.DisplayName;
  24.                         ////item.SaveAsFile(this.GetAttachmentPath(item.FileName, Properties.Settings.Default.ReceiveAttachmentPath));
  25.                         item.SaveAsFile(this.GetAttachmentPath(item.FileName, "c:\\test\\attachments\\"));
  26.                     }
  27.                     myItem.SubItems.Add(AttachmentNames);
  28.                     InboxMailItem.Delete();
  29.                     InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.FindNext();
  30.                 }
  31.  
  32.                ////MailMsg.Send();
  33.                 MailNS.Logoff();
  34.                 MailNS = null;
  35.                 MyInbox = null;
  36.                 InboxMailItem = null;
  37.                 ((Microsoft.Office.Interop.Outlook._Application)MyApp).Quit();
  38.                 ////MyApp.Quit();
  39.                 MyApp = null;
  40.             }
  41.             catch (Exception ex)
  42.             {
  43.                 MessageBox.Show(ex.Message);
  44.             }

the getattachmentpath method implementation is below. This method will check for any existing attachment with the same name and adds running number if exists

Code Snippet
  1. private string GetAttachmentPath(string AttachfileName, string AttachPath)
  2.         {
  3.             string filename = Path.GetFileNameWithoutExtension(AttachfileName);
  4.             string ext = Path.GetExtension(AttachfileName);
  5.             int app = 0;
  6.             if (File.Exists(AttachPath + filename + ext))
  7.             {
  8.                 while (File.Exists(AttachPath + filename + app + ext))
  9.                 {
  10.                     app++;
  11.                 }
  12.                 filename = filename + app;
  13.             }
  14.             return AttachPath + filename + ext;
  15.         }

1 thought on “Read Outlook email by filter criteria

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s