C# EWS Api on Exchange 2013 : Get an attachment into a Stream
I want to get attached files from a mail into streams type. But how to create the stream ? I'm correctly getting mail items (content, subject, attached files). Referring to the following link : EWS Managed API : Getting attachment I tried to do the following :
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);
}
}
Ok don't worry I make a lot of test and manage exceptions but it's not relevant to put all the code here.
Some precisions :
I found this post : MSDN Forum and trying the following method :
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();
(Here my text is broken as I cannot convert it as code format so sorry for the italics ... out of luck today)
But it search the attachment on the local drive ...
Please help me
Basically I can't get how my attachment[i] can be added to a FileStream var...
Thanks a lot, really.
If I understand correctly you want to save the attachement somewhere? EWS provides you a byte array for every file stored within Content property of FileAttachment object, and from there its extremely easy to do so:
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);
}
}
}
If you just want a Stream object to do something else with it just create a MemoryStream:
MemoryStream ms = new MemoryStream(fa.Content);
Thanks to @anusiak :),
Here is my code concerning the stream :
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();
And I had to call Load() method for each attachment extracted from a mail, before I can store them into a FileAttachment var. I then use a MemoryStream to add the data stream into a special type that I needed to use. The rest of the code is about adding it to an item from a SharePoint list, so no need to explain.
链接地址: http://www.djcxy.com/p/49146.html