Monthly Archives: October 2010

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.         }

.NET Remoting – Communicate between application

There are situation we need to communicate between application over network or in the same machine. There are several ways to implement the functionality. To communicate between application we have few options like .NEt Named Pipes, MSMQ, Enterprise services and Remoting.

Depends on the requirement we can select the communication option. In this blog i will discuss how to implement kind of chat application using .NET Remoting.

.NET Remoting Overview 

The .NET remoting infrastructure is an abstract approach to inter-process communication. Objects that can be passed by value, or copied, are automatically passed between applications in different application domains or on different computers. Mark your custom classes as serializable to make this work.

Can read more on Remoting here.

Sample Code

The sample application which i am posting here has 3 major component to it

  1. Remoting Server
  2. Remoting Client
  3. Remoting Library

Remoting Server:

The Remoting Server module will open the connection using Tcp Channel on the specified port. The server also can send message to multiple client which are connected to receive the message.

image

Server Application Exe

Remoting Client:

The Remoting client will try to connect to the server and respond back with the message.

image

Client Application Exe

Remoting Library:

Remoting Library will have the Remotable Type object with the Message Response and the Message Transfer class type. Both the class types are serializable

Full source is here