我使用EWS托管API下载电子邮件,我设法自行下载电子邮件。我可以通过EmailMessage.From.Name和EmailMessage.Subject验证它是否是正确的电子邮件。
然而,当在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 = @"foo@bar.com";
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 :(得分:0)
我还没有使用过EWS,所以这可能是错误的。
我已经查看了电子邮件的正文。附件实际上在身体中传输,通常作为base64编码的字符串,在编码数据之前和之后使用mime标记来区分不同的文件。 EWS可能没有在您获得的对象中处理这些内容。有可能的是,当您获得一个项目并投射到EnailMessage时,EmailMessage还没有完全处理到例如提取附件。请注意,文档说:
这可能会有所帮助。