我使用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();
}
}
}
此處顯示:Console.WriteLine(「From:{0} {1} Subject:{2} {3} Number of Attachments:{4}」,newestInventoryReport.From.Name,Environment.NewLine,newestInventoryReport。 Subject,Environment.NewLine,newestInventoryReport.Attachments.Count);' –
來自:Christell,Canin主題:foobar主題附件數量:0 –