从Outlook 2007下载电子邮件附件

时间:2011-01-05 15:51:52

标签: c#

我是新的2 C#,我已经完成了一项任务...... 我必须编写一个C#代码,将outlook 2007中的电子邮件附件下载到本地驱动器或任何指定位置。该程序应该以这样的方式,给定任何用户名和密码,它应连接到该特定用户的Outlook并下载从特定的地址或主题行指定的文件。 任何形式的帮助都表示赞赏。

2 个答案:

答案 0 :(得分:0)

那么您在Exchange 2007/2010环境中使用Outlook?如果是,你冷看看EWS

答案 1 :(得分:0)

浏览以下代码。它应该工作!

        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;
        }
相关问题