如何在 C# 中使用 EWS API 2.0 从电子邮件线程下载所有附件
How to download all attachments from email threads using EWS API 2.0 in C#
我已成功设置并从 EWS 下载附件,这是我正在处理的简短代码:
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)
{
string sFilePath;
FileAttachment fileAttachment = attachment as FileAttachment;
我遇到了一个问题,它只从最新的电子邮件线程而不是以前的线程下载附件。看看下面的电子邮件场景,我向我的朋友发送了一封带有附件 1 的电子邮件,他用附件 2 回复了我。我如何从电子邮件中检索附件并与它们所属的电子邮件线程相关联。
电子邮件场景:
This is Second thread with new attachment
Attachment 2
On Tue, Sep 1, 2015 at 9:53 PM, stash it
> wrote:
Checking Document Attachment
Attachment 1
我想通了:
ConversationId convId = item.ConversationId;
PropertySet properties = new PropertySet(BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.InReplyTo,
ItemSchema.DateTimeReceived,
ItemSchema.DateTimeSent,
ItemSchema.DisplayCc,
ItemSchema.IsFromMe,
ItemSchema.DisplayTo,
ItemSchema.HasAttachments,
ItemSchema.Attachments,
ItemSchema.UniqueBody);
// Request conversation items. This results in a call to the service.
ConversationResponse response = service.GetConversationItems
(convId,properties,null,null,
ConversationSortOrder.TreeOrderDescending);
foreach (ConversationNode node in response.ConversationNodes)
{
foreach (Item item in node.Items)
{
Console.WriteLine(" Received: " + item.DateTimeReceived);
Console.WriteLine(" Received: " + item.uniquebody);
Console.WriteLine(" Received: " + item.subject);
if (item.HasAttachments)
{
foreach(Attachment attach in item.Attachments)
{
FileAttachment fileAttachment = attach as FileAttachment;
fileAttachment.Load(sFilePath);
}
}
}
}
参考:
https://msdn.microsoft.com/en-us/library/office/dn610351(v=exchg.150).aspx
我已成功设置并从 EWS 下载附件,这是我正在处理的简短代码:
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)
{
string sFilePath;
FileAttachment fileAttachment = attachment as FileAttachment;
我遇到了一个问题,它只从最新的电子邮件线程而不是以前的线程下载附件。看看下面的电子邮件场景,我向我的朋友发送了一封带有附件 1 的电子邮件,他用附件 2 回复了我。我如何从电子邮件中检索附件并与它们所属的电子邮件线程相关联。
电子邮件场景:
This is Second thread with new attachment
Attachment 2
On Tue, Sep 1, 2015 at 9:53 PM, stash it > wrote:
Checking Document Attachment
Attachment 1
我想通了:
ConversationId convId = item.ConversationId;
PropertySet properties = new PropertySet(BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.InReplyTo,
ItemSchema.DateTimeReceived,
ItemSchema.DateTimeSent,
ItemSchema.DisplayCc,
ItemSchema.IsFromMe,
ItemSchema.DisplayTo,
ItemSchema.HasAttachments,
ItemSchema.Attachments,
ItemSchema.UniqueBody);
// Request conversation items. This results in a call to the service.
ConversationResponse response = service.GetConversationItems
(convId,properties,null,null,
ConversationSortOrder.TreeOrderDescending);
foreach (ConversationNode node in response.ConversationNodes)
{
foreach (Item item in node.Items)
{
Console.WriteLine(" Received: " + item.DateTimeReceived);
Console.WriteLine(" Received: " + item.uniquebody);
Console.WriteLine(" Received: " + item.subject);
if (item.HasAttachments)
{
foreach(Attachment attach in item.Attachments)
{
FileAttachment fileAttachment = attach as FileAttachment;
fileAttachment.Load(sFilePath);
}
}
}
}
参考: https://msdn.microsoft.com/en-us/library/office/dn610351(v=exchg.150).aspx