C#下载电子邮件-找不到附件

本文关键字:找不到 下载 电子邮件 | 更新日期: 2023-09-27 17:59:36

我正在使用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();
        }
    }
}

C#下载电子邮件-找不到附件

我没有使用EWS,所以这可能是错误的。

我得看看邮件的正文。附件实际上是在正文中传输的,通常是base64编码的字符串,在编码数据之前和之后都有mime标记,以区分不同的文件。EWS可能没有在您获得的对象中处理这些。有可能,当您获得一个Item并转换为EnailMessage时,EmailMessage还没有得到完全处理,例如提取附件。请注意,文档上写着:

使用Bind(ExchangeService,ItemId)方法绑定到Exchange服务器上的现有电子邮件。

这可能会有所帮助。