gpt4 book ai didi

c# - 如何在 EWS API 上下载特定文件附件

转载 作者:行者123 更新时间:2023-11-30 16:15:41 26 4
gpt4 key购买 nike

这就是我目前所拥有的,使用这段代码我可以下载邮件中找到的所有附件,但我真正需要的是下载两个特定的文件扩展名附件(.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);
}
}

最佳答案

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

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 并忽略所有其他内容。

关于c# - 如何在 EWS API 上下载特定文件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19322661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com