开发者

Download Email Attachments from Outlook 2007

I am new 2 C# and i have been given a task... I have to write a C# code to download the email attachments from outlook 2007 to a local drive or any specified location.The program should be in such a way that, given any username and password it should connect to that particu开发者_如何学编程lar users outlook and download the files specified from a particular from address or subject line. Any kind of help is appreciated.


So you are using outlook in an Exchange 2007/2010 environment? If yes you cold take a look at EWS.


Go through the following piece of code. It should work!

        Microsoft.Office.Interop.Outlook.Application app = null;
        Microsoft.Office.Interop.Outlook._NameSpace ns = null;
        Microsoft.Office.Interop.Outlook.PostItem item = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
        //Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

        try 
        {
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("MAPI");
            ns.Logon(null,null,false, false);
            inboxFolder = ns.GetDefaultFolder          (Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            //subFolder = inboxFolder.Folders["MySubFolderName"]; 
            //folder.Folders[1]; also works
            //Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
            //Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

            for (int i = 1; i <= inboxFolder.Items.Count; i++)
            {
                item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];                   
                foreach (Microsoft.Office.Interop.Outlook.Attachments attachment in item.Attachments)
                {
                    // Process the "attachment" object as per your requirement!
                }

             //Console.WriteLine("Item: {0}", i.ToString());
             //Console.WriteLine("Subject: {0}", item.Subject); 
             //Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
             //Console.WriteLine("Categories: {0}", item.Categories);
             //Console.WriteLine("Body: {0}", item.Body);
             //Console.WriteLine("HTMLBody: {0}", item.HTMLBody); 
            }
        } 
        catch (System.Runtime.InteropServices.COMException ex) 
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            ns = null;
            app = null;
            inboxFolder = null;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜