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
Once added please copy paste the below code to run the sample
- Microsoft.Office.Interop.Outlook.Application MyApp = new Microsoft.Office.Interop.Outlook.Application();
- Microsoft.Office.Interop.Outlook.NameSpace MailNS = MyApp.GetNamespace("mapi");
- MailNS.Logon(MyApp.DefaultProfileName, Missing.Value, false, true);
- Microsoft.Office.Interop.Outlook.MAPIFolder MyInbox = null;
- MyInbox = MailNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
- Microsoft.Office.Interop.Outlook._MailItem InboxMailItem = null;
- Microsoft.Office.Interop.Outlook.Items oItems = MyInbox.Items;
- ////string Query = "[Subject] = '" + Properties.Settings.Default.EmailSubject + "'";
- string Query = "[Subject] = 'my sample search text'";
- InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.Find(Query);
- while (InboxMailItem != null)
- ListViewItem myItem = lvwMails.Items.Add(InboxMailItem.SenderName);
- string AttachmentNames = string.Empty;
- foreach (Microsoft.Office.Interop.Outlook.Attachment item in InboxMailItem.Attachments)
- AttachmentNames += item.DisplayName;
- ////item.SaveAsFile(this.GetAttachmentPath(item.FileName, Properties.Settings.Default.ReceiveAttachmentPath));
- item.SaveAsFile(this.GetAttachmentPath(item.FileName, "c:\\test\\attachments\\"));
- InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.FindNext();
- MailNS = null;
- MyInbox = null;
- InboxMailItem = null;
- MyApp = null;
- catch (Exception ex)
the getattachmentpath method implementation is below. This method will check for any existing attachment with the same name and adds running number if exists
- private string GetAttachmentPath(string AttachfileName, string AttachPath)
- string filename = Path.GetFileNameWithoutExtension(AttachfileName);
- string ext = Path.GetExtension(AttachfileName);
- int app = 0;
- if (File.Exists(AttachPath + filename + ext))
- while (File.Exists(AttachPath + filename + app + ext))
- filename = filename + app;
- return AttachPath + filename + ext;
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.
The sample application which i am posting here has 3 major component to it
- Remoting Server
- Remoting Client
- Remoting Library
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.
Server Application Exe
The Remoting client will try to connect to the server and respond back with the message.
Client Application Exe
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