Exchange 2013上的C#EWS Api:将附件获取到流中
我想将邮件中的附加文件转换为流类型。但是如何创建流呢? 我正确获取邮件项目(内容、主题、附件)。 参考以下链接: 我试着做到以下几点:Exchange 2013上的C#EWS Api:将附件获取到流中,c#,stream,attachment,exchangewebservices,C#,Stream,Attachment,Exchangewebservices,我想将邮件中的附加文件转换为流类型。但是如何创建流呢? 我正确获取邮件项目(内容、主题、附件)。 参考以下链接: 我试着做到以下几点: int nbAttachments = message.Attachments.Count; FileAttachment[] attachedFiles = new FileAttachment[nbAttachments]; for (int i=0; i < nbAttachments; i++) { attachedFiles[i] =
int nbAttachments = message.Attachments.Count;
FileAttachment[] attachedFiles = new FileAttachment[nbAttachments];
for (int i=0; i < nbAttachments; i++)
{
attachedFiles[i] = message.Attachments[i] as FileAttachment;
}
for (int i = 0; i < attachments.Length; i++)
{
if (attachments[i].Name != null)
{
AttachmentCreationInformation infoAttachment = new AttachmentCreationInformation();
attachments[i].Load(infoAttachment.ContentStream);
infoAttachment.FileName = attachments[i].Name;
newItem.AttachmentFiles.Add(infoAttachment);
}
}
int nbAttachments=message.Attachments.Count;
FileAttachment[]attachedFiles=新文件附件[nbAttachments];
对于(int i=0;i
好的,别担心,我做了很多测试和管理异常,但是把所有代码放在这里并不重要
某些精度:
- newItem是来自SharePoint网站的列表项。我需要一个AttachmentCreationInformation类型来将附加文件添加到此项目
- 我找到了这篇文章:并尝试了以下方法: FileStream stream=新FileStream(附件[i]。名称,FileMode.Open) byte[]byteArray=新字节[stream.Length] stream.Read(byteArray,0,Convert.ToInt32(stream.Length)) stream.Close()
真的非常感谢。如果我理解正确,你想把附件保存在某个地方吗?EWS为存储在FileAttachment对象的Content属性中的每个文件提供一个字节数组,从这里可以非常轻松地执行此操作:
foreach (var a in mail.Attachments)
{
FileAttachment fa = a as FileAttachment;
if(fa != null)
{
try
{
//if you don't call this the Content property may be null,
//depending on your property loading policy with EWS
fa.Load();
}
catch
{
continue;
}
using(FileStream fs = System.IO.File.OpenWrite("path_to_file"))
{
fs.Write(fa.Content, 0, fa.Content.Length);
}
}
}
如果您只想让流对象对其执行其他操作,只需创建一个MemoryStream:
MemoryStream ms = new MemoryStream(fa.Content);
感谢@anusiak:)
以下是我关于流的代码:
for (int i = 0; i < attachments.Length; i++)
{
if (attachments[i].Name != null && attachments[i].Content != null)
{
MemoryStream mstream = new MemoryStream(attachments[i].Content);
AttachmentCreationInformation spAttachment = new AttachmentCreationInformation();
spAttachment.ContentStream = mstream;
spAttachment.FileName = attachments[i].Name;
newItem.AttachmentFiles.Add(spAttachment);
}
}
newItem.Update();
for(int i=0;i
我必须为从邮件中提取的每个附件调用Load()方法,然后才能将它们存储到FileAttachment变量中。然后,我使用MemoryStream将数据流添加到我需要使用的特殊类型中。代码的其余部分是关于将其添加到SharePoint列表中的项目,因此无需解释