gpt4 book ai didi

Exchange 2013 上的 C# EWS Api : Get an attachment into a Stream

转载 作者:行者123 更新时间:2023-11-30 13:37:32 26 4
gpt4 key购买 nike

我想将邮件中的附件转换为流类型。但是如何创建流?我正确地收到了邮件项目(内容、主题、附件)。引用以下链接: EWS Managed API : Getting attachment我尝试执行以下操作:

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);
}
}

好的,别担心,我做了很多测试和管理异常,但将所有代码都放在这里并不重要。

一些精度:

  • newItem 是来自 SharePoint 网站的列表项。我需要一个 AttachmentCreationInformation 类型来为这个项目添加一个附件。
  • 我找到这篇文章:MSDN Forum并尝试以下方法:

    FileStream stream = new FileStream(attachments[i].Name, FileMode.Open);

    byte[] byteArray = new byte[stream.Length];

    stream.Read(byteArray, 0, Convert.ToInt32(stream.Length));

    stream.Close();

(这里我的文字被破坏了,因为我无法将它转换为代码格式,所以很抱歉斜体......今天运气不好)

但是它搜索本地驱动器上的附件...

请帮帮我

基本上我不知道如何将我的附件 [i] 添加到 FileStream var...


非常感谢,真的。

最佳答案

如果我没理解错的话,您想将附件保存在某个地方? 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);
}
}
}

如果您只是想让一个 Stream 对象用它做一些其他事情,只需创建一个 MemoryStream:

MemoryStream ms = new MemoryStream(fa.Content);

关于Exchange 2013 上的 C# EWS Api : Get an attachment into a Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22376025/

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