2014-04-22 63 views
0

我使用EWS託管API下載電子郵件,並且我設法無故下載電子郵件。我可以通過EmailMessage.From.Name和EmailMessage.Subject驗證它是否是正確的電子郵件。C#電子郵件下載 - 附件未找到

但是,在Outlook中查看它時有一個附件,但.Net對象的EmailMessage.HasAttachments設置爲false,並且EmailMessage.Attachments.Count等於零。

爲什麼我能夠在Outlook中看到附件,但不能通過.Net對象?

代碼我有如下:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.IO.Compression; 
using System.Linq; 
using Microsoft.Exchange.WebServices.Data; 

namespace CiscoPrimeInfrastructureDataAccess 
{ 
    public static class EmailFetcher 
    { 
     private const string _EMAIL = @"[email protected]"; 
     private const string _USERNAME = "foo"; 
     private const string _PASSWORD = "bar123"; 
     private const string _DOMAIN = "bar"; 
     private const string _SUBJECT = "foobar subject"; 

     public static string GetCurrentPrimeCsv() 
     { 
      string inboxFolder = ConfigurationManager.AppSettings["Inbox"]; 

      if (!File.Exists(inboxFolder)) 
       File.Create(inboxFolder); 

      var service = CreateExchangeService(); 

      var newestInventoryReport = GetNewestInventoryEmail(service); 

      Console.WriteLine("From: {0}{1}Subject: {2}{3}Number of Attachments: {4}", newestInventoryReport.From.Name, Environment.NewLine, newestInventoryReport.Subject, Environment.NewLine, newestInventoryReport.Attachments.Count); 

      FileAttachment attachment = newestInventoryReport.Attachments[0] as FileAttachment; 

      string guid = Guid.NewGuid().ToString(); 

      string downloadFilePath = Path.Combine(inboxFolder, guid + "_" + attachment.FileName); 
      string extractedFilePath = Path.Combine(inboxFolder, guid + "_extracted.csv"); 

      attachment.Load(Path.Combine(downloadFilePath)); 

      ZipFile.ExtractToDirectory(downloadFilePath, extractedFilePath); 

      string csv = String.Empty; 

      using (var reader = new StreamReader(extractedFilePath)) 
      { 
       csv = reader.ReadToEnd(); 
      } 

      return csv; 
     } 



     private static ExchangeService CreateExchangeService() 
     { 
      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
      service.Credentials = new WebCredentials(_USERNAME, _PASSWORD, _DOMAIN); 

      service.AutodiscoverUrl(_EMAIL); 

      return service; 
     } 


     private static EmailMessage GetNewestInventoryEmail(ExchangeService service) 
     { 
      var firstSeveralEmails = GetFirstSeveralEmails(service); 

      EmailMessage newestEmail = null; 

      foreach (var email in firstSeveralEmails) 
      { 
       if (email.Subject.Contains(_SUBJECT)) 
       { 
        if (newestEmail == null || email.DateTimeReceived > newestEmail.DateTimeReceived) 
        { 
         newestEmail = email; 
        } 
       } 
      } 

      return newestEmail; 
     } 

     private static IEnumerable<EmailMessage> GetFirstSeveralEmails(ExchangeService service) 
     { 
      Mailbox mailbox = new Mailbox(_EMAIL); 
      FolderId inboxFolderId = new FolderId(WellKnownFolderName.Inbox, mailbox); 

      FindItemsResults<Item> findItemsResults = service.FindItems(inboxFolderId, new ItemView(128)); 

      return findItemsResults.OfType<EmailMessage>().Select(findEmailResult => findEmailResult as EmailMessage).ToList(); 
     } 
    } 
} 
+0

此處顯示:Console.WriteLine(「From:{0} {1} Subject:{2} {3} Number of Attachments:{4}」,newestInventoryReport.From.Name,Environment.NewLine,newestInventoryReport。 Subject,Environment.NewLine,newestInventoryReport.Attachments.Count);' –

+0

來自:Christell,Canin 主題:foobar主題附件數量:0 –

回答

0

我沒有使用EWS所以這可能是錯誤的。

我會看看電子郵件的正文。附件實際上是在身體中傳輸的,通常是base64編碼的字符串,在編碼數據前後使用mime標記來區分不同的文件。 EWS可能沒有在你得到的對象中處理這些。可能的是,當您收到一個Item並將其轉換爲EnailMessage時,EmailMessage尚未完全處理爲例如提取附件。請注意,文件說:

Use the Bind(ExchangeService, ItemId) method to bind to an existing e-mail message on the Exchange server.

這可能會有幫助。