Software Architecture – To read emails with attachments via C# 4.0 + Office Interop Outlook

Objective:
Objective of this code is to read attachments from emails in MS outlook and save in a folder on hard disk.

Environment:
MS Visual Studio 2010 + C# 4.0 + Office Interop Outlook
Solutiuon:
The types and members of the Microsoft.Office.Interop.Outlook namespace provide support for interoperability between the COM object model of Microsoft Outlook and managed applications that automate Outlook.

 | oApp = new Microsoft.Office.Interop.Outlook.Application();
            oNameSpace = oApp.GetNamespace("MAPI");
            oNameSpace.Logon(null, null, true, true);
            oInboxFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            foreach (var item in oInboxFolder.Items)
            {
                Microsoft.Office.Interop.Outlook._MailItem oMailItem = item as Microsoft.Office.Interop.Outlook._MailItem;                
                Microsoft.Office.Interop.Outlook.Attachments ii = oMailItem.Attachments;
                for (int j = 1; j <= ii.Count; j++)
                {
                Microsoft.Office.Interop.Outlook.Attachment aa = (Microsoft.Office.Interop.Outlook.Attachment)ii[j];
                string mypath = @"F:\\FileOutbox\\" + aa.FileName;
                mypath = @mypath;
                aa.SaveAsFile(mypath);
                }
                oMailItem.Delete();
            }
            

Related Post: https://softarchitect.wordpress.com/2010/09/18/software-architecture-sync-data-between-offlinedisconnected-databases-ssis-bids/

Advertisements

One Response to Software Architecture – To read emails with attachments via C# 4.0 + Office Interop Outlook

  1. Rupali says:

    Thanks very much its very useful to me

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: