Reputation: 5977
I am trying to run the below code to write an item that is an email attachment to a file on disk but it gives an error because the property is not loaded.
using (Stream FileToDisk = new FileStream(emailAttachmentsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
itemAttachment.Load(
byte[] ContentBytes = System.Convert.FromBase64String(itemAttachment.ContentType);
FileToDisk.Write(ContentBytes, 0,ContentBytes.Length);
FileToDisk.Flush();
FileToDisk.Close();
}
I have tried this but it won't compile:
email.Load(new PropertySet(ItemSchema.MimeContent));
how can I get the item Content type so I can create my ContentBytes
object?
Upvotes: 1
Views: 7828
Reputation: 102
itemAttachment.Load(new PropertySet(EmailMessageSchema.MimeContent));
var mimeContent2 = itemAttachment.Item.MimeContent;
fileBytes = mimeContent2.Content;
Upvotes: 2
Reputation: 5977
in the end I did this (basically I needed to load the Mimi content of the ietem attachment, plus some extra properties:
itemAttachment.Load(new PropertySet(BasePropertySet.FirstClassProperties));
itemAttachment.Load(new PropertySet(
BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.DateTimeReceived,
ItemSchema.DisplayTo,
EmailMessageSchema.From, EmailMessageSchema.Sender,
EmailMessageSchema.HasAttachments, ItemSchema.MimeContent,
EmailMessageSchema.Body, EmailMessageSchema.Sender,
ItemSchema.Body) { RequestedBodyType = BodyType.Text });
string BodyText = itemAttachment.Item.Body.Text;
string itemAttachmentName = itemAttachment.Name.Replace(":", "");
// Put attachment contents into a stream.
emailAttachmentsPath = emailAttachmentsPath + "\\" + itemAttachmentName + ".eml";
var mimeContent = itemAttachment.Item.MimeContent;
//save to disk
using (var fileStream = new FileStream(emailAttachmentsPath, FileMode.Create))
{
fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
}
another option:
var mimeContent2 = itemAttachment.Item.MimeContent;
byte[] ContentBytes = mimeContent2.Content;
//convert array of bytes into file
FileStream objfileStream = new FileStream("C:\\email2case\\temp\\testing" + System.DateTime.Now.ToString("HHmmss") + ".eml", FileMode.Create);
objfileStream.Write(ContentBytes, 0, ContentBytes.Length);
objfileStream.Close();
I hope this helps someone!
Upvotes: 1