3

这是我到目前为止所拥有的,问题是使用此代码我可以下载消息中找到的所有附件,但我真正需要的是下载两个特定的文件扩展名附件(.PDF 和 .XML,IE),任何小费将非常感激:

static void Main(string[] args)
    {
        try
        {
            ExchangeService service = new      ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new NetworkCredential("USR", "PWD", "domain");
            service.AutodiscoverUrl("someone@example.com");

            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
            Console.WriteLine("Found " + findResults.TotalCount + " Messages");

                if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
                    foreach (Item item in findResults.Items)

                    {
                        EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
                        foreach (Attachment attachment in message.Attachments)
                        {
                            if (attachment is FileAttachment)
                            if (attachment.Name.Contains(".xml"))
                            {
                                FileAttachment fileAttachment = attachment as FileAttachment;
                                fileAttachment.Load("c:\\folder\\" + fileAttachment.Name);
                                Console.WriteLine("Attachment name: " + fileAttachment.Name);
                            }
                        }
                        Console.WriteLine(item.Subject);
                    }
                else
                    Console.WriteLine("No messages");
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
        }
    }
4

1 回答 1

3

将代码编辑/评论移动到答案中。

if (attachment is FileAttachment)
{
         if (attachment.Name.EndsWith(".xml") || attachment.Name.EndsWith(".pdf"))
         {
             FileAttachment fileAttachment = attachment as FileAttachment;
             fileAttachment.Load("c:\\folder\\" + fileAttachment.Name);
             Console.WriteLine("Attachment name: " + fileAttachment.Name);
         }
}

解决方案只是在下载之前检查文件名是否包含 .xml 或 .pdf 并忽略所有其他文件。

于 2014-05-20T20:44:04.207 回答