从Outlook 2007下载电子邮件附件

问题描述:

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

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

+0

感谢您的快速回复......我加入了Microsoft.exchange.webservices参考后得到休耕错误。错误无法找到类型或命名空间名称'ExchangeServiceBinding'(您是否缺少using指令或程序集引用?) – 2011-01-05 16:35:03

+0

您需要通过Visual Studio中的“添加服务引用”来添加Webservice。 – schwindelig 2011-01-05 16:50:23

+0

那帮了我...谢谢你:) – 2011-01-05 18:07:42

请仔细阅读以下代码。它应该工作!

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