每日从邮箱下载附件

时间:2017-12-01 12:18:38

标签: c# exchangewebservices

我想每天检查邮箱并下载所有邮件的附件。 现在,当我手动设置ItemId时,我可以为一封电子邮件执行此操作。我的foreach循环没有运行。我在循环中得到System.ArgumentOutOfRangeException

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

在XML中,我可以清楚地看到ItemId是正确的,并且还有一个附件。

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

service.Credentials = new NetworkCredential("user", "pwd", "domain");

service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;

service.Url = new Uri("https://fqdn_of_server/EWS/exchange.asmx");

PropertySet itempropertyset = new PropertySet(BasePropertySet.IdOnly, ItemSchema.HasAttachments);

ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;

SearchFilter searchFilter = new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-1));

findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

foreach (EmailMessage message in findResults)
{
    EmailMessage.Bind(service, new ItemId(message.Id.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
    FileAttachment attachment = message.Attachments[0] as FileAttachment;
    FileAttachment fileAttachment = attachment as FileAttachment;
    fileAttachment.Load();
    fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
}

我正在使用ews v2.0.50727

EDIT ___________

此特定电子邮件的代码工作正常。但我无法将其调整为循环。所以我想这是我的查询只是每日邮件的问题。

EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkADM1MWIyYjA4LWViNDEtNGQ2OS1hMDlkLTdiMDY2YTU0Y2MzMwBGAAAAAABO/r/HLJjWR6+SA9a3YgeTBwDUar3a2c6dRJoGCSVHCKVoAAAARWONAADUar3a2c6dRJoGCSVHCKVoAAAAYCsXAAA="), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

foreach (Attachment attachment in message.Attachments)
    {
        Debug.WriteLine("Attachement: " + attachment);
        if (attachment is FileAttachment)
        {
            FileAttachment fileAttachment = attachment as FileAttachment;
            Debug.WriteLine("Attachement: " + attachment);

            // Load the file attachment into memory and print out its file name.
            fileAttachment.Load();
            Console.WriteLine("Attachment name: " + fileAttachment.Name);

            // Load attachment contents into a file.
            fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

            // Stream attachment contents into a file.
            FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fileAttachment.Load(theStream);
            theStream.Close();
            theStream.Dispose();
        }
        else // Attachment is an item attachment.
        {
            // Load attachment into memory and write out the subject.
            ItemAttachment itemAttachment = attachment as ItemAttachment;
            itemAttachment.Load();
            Debug.WriteLine("Attachement: " + attachment);
            Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
        }
    }

2 个答案:

答案 0 :(得分:3)

您的代码存在一些问题。 您没有遍历附件集合并加载每个附件。 你也不应该使用这段代码:

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

此代码只获取附件集合中的第一个附件。

尝试将foreach代码更改为以下代码:

foreach (EmailMessage message in findResults)
{
    EmailMessage.Bind(service, new ItemId(message.Id.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

    // Iterate through the attachments collection and load each attachment.
    foreach (Attachment attachment in message.Attachments)
    {
        if (attachment is FileAttachment)
        {
            // Load the file attachment into memory and print out its file name.
            fileAttachment.Load();
            Console.WriteLine("Attachment name: " + fileAttachment.Name);

            // Load attachment contents into a file.
            fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

            // Stream attachment contents into a file.
            FileStream theStream = new FileStream("C:\\temp\\Stream_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fileAttachment.Load(theStream);
            theStream.Close();
            theStream.Dispose();
        }
        else // Attachment is an item attachment.
        {
            // Load attachment into memory and write out the subject.
            ItemAttachment itemAttachment = attachment as ItemAttachment;
            itemAttachment.Load();
            Console.WriteLine("Subject: " + itemAttachment.Item.Subject);
        }
    }
}

答案 1 :(得分:1)

找到解决方案。我认为问题是DateTime

以下是工作代码:

itempropertyset