我正在尝试使用以下代码使用C#和Exchange Web服务从收件箱中的电子邮件连接和下载附件,但我收到'System.ArgumentOutOfRangeException'错误,我不明白为什么。我已经google了一个答案,但我找不到一个或我找到的答案是非常旧版本的EWS。
我知道其他代码通常有效,因为我可以访问与电子邮件相关的其他信息,而不是访问附件。
任何人都会向我展示我的方式错误吗?
提前致谢,
詹姆斯
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new NetworkCredential("MYLOGIN", "MYPASSWORD", "MYDOMAIN");
service.Url = new Uri("https://MYMAILSERVER/EWS/Exchange.asmx");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));
foreach (Item item in findResults.Items)
{
if (item.HasAttachments && item.Attachments[0] is FileAttachment)
{
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
}
}
}
}
已解决但新问题
我现在通过将'foreach(findResults.Items中的项目项)更改为'foreach(findResults.Items中的EmailMessage项)'来排序问题,但现在我需要了解如何通过附件进行枚举 - 任何任何人的想法?
答案 0 :(得分:7)
检查您的个人资料。如果您在轻型模式下运行,则不会使用消息下载附件。
添加以下行
item.Load() // loads the entire message with attachment
答案 1 :(得分:2)
您的新问题的答案是
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
service.AutodiscoverUrl("firstname.lastname@MyCompany.com");
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView(10));
foreach (Item item in findResults.Items)
{
Console.WriteLine(item.Subject);
item.Load();
if(item.HasAttachments)
{
foreach (var i in item.Attachments)
{
FileAttachment fileAttachment = i as FileAttachment;
fileAttachment.Load();
Console.WriteLine("FileName: " + fileAttachment.Name);
}
}
}
答案 2 :(得分:1)
除非我遗漏了一些显而易见的事情,否则您只需通过item.Attachments
枚举。
点击here,然后向下滚动到您看到Example
标题的位置。
答案 3 :(得分:1)
从指定数量的电子邮件下载所有附件的解决方案:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new NetworkCredential("login", "password");
service.Url = new Uri("https://mail.Yourservername.com/EWS/Exchange.asmx");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
foreach (EmailMessage item in findResults)
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
foreach (Attachment attachment in message.Attachment
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load(@"Folder\file.name");
}
}
}
不要忘记将正确版本的Exchange Server传递给ExchangeService构造函数。
答案 4 :(得分:0)
这是一种可用于下载附件的GetAttachmentsFromEmail方法。
public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId)
{
// Bind to an existing message item and retrieve the attachments collection.
// This method results in an GetItem call to EWS.
EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the attachment into a file.
// This call results in a GetAttachment call to EWS.
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
Console.WriteLine("File attachment name: " + fileAttachment.Name);
}
else // Attachment is an item attachment.
{
ItemAttachment itemAttachment = attachment as ItemAttachment;
// Load attachment into memory and write out the subject.
// This does not save the file like it does with a file attachment.
// This call results in a GetAttachment call to EWS.
itemAttachment.Load();
Console.WriteLine("Item attachment name: " + itemAttachment.Name);
}
}
}