none
Read Data.EmailMessage to memorystream? RRS feed

  • Question

  • Hi all,

    I'm working with the Webservices API to retrieve email messages from an Exchange mailbox and then store them in SharePoint. Ideally I need to read a Data.EmailMessage to a memorystream, so I can upload it SP. However, the EmailMessage type is not serializable.

    Is there another way to convert an EmailMessage to a stream?

    This is the code I was trying to use which fails...

    using (MemoryStream ms = new MemoryStream())
    
       {
    
        BinaryFormatter bf = new BinaryFormatter();
    
        bf.Serialize(ms, item);
    
    
    
    ... 
    
    

    where item is of type EmailMessage.

    I know that FileAttachments have a Load method which will read to a stream, but I can't see anything on the EmailMessage type that will do this.

    Thanks.



    Wednesday, June 29, 2011 3:29 PM

Answers

  • I have a way:

                MemoryStream memorystream = new MemoryStream();

                byte[] content = msg.MimeContent.Content;
                memorystream.Position = 0;
                memorystream.Read(content, 0, (int)memorystream.Length);

     

    It has drawbacks in that it will only return the body of the email, it won't give you things like who its From, To, DateSent, DateReceived etc.

     

    • Marked as answer by weirdbeardmt Wednesday, June 29, 2011 4:34 PM
    Wednesday, June 29, 2011 4:12 PM
  • Hi

    Thanks. I'd actually arrived at the same thing. Although, my method gives me the whole message:

     PropertySet ps = new PropertySet(ItemSchema.MimeContent); item.Load(ps); using (MemoryStream ms = new MemoryStream(item.MimeContent.Content)) {


    If I save the item as an .eml file, then I get everything, including attachments.


    • Marked as answer by weirdbeardmt Wednesday, June 29, 2011 4:34 PM
    Wednesday, June 29, 2011 4:33 PM

All replies

  • I have a way:

                MemoryStream memorystream = new MemoryStream();

                byte[] content = msg.MimeContent.Content;
                memorystream.Position = 0;
                memorystream.Read(content, 0, (int)memorystream.Length);

     

    It has drawbacks in that it will only return the body of the email, it won't give you things like who its From, To, DateSent, DateReceived etc.

     

    • Marked as answer by weirdbeardmt Wednesday, June 29, 2011 4:34 PM
    Wednesday, June 29, 2011 4:12 PM
  • Hi

    Thanks. I'd actually arrived at the same thing. Although, my method gives me the whole message:

     PropertySet ps = new PropertySet(ItemSchema.MimeContent); item.Load(ps); using (MemoryStream ms = new MemoryStream(item.MimeContent.Content)) {


    If I save the item as an .eml file, then I get everything, including attachments.


    • Marked as answer by weirdbeardmt Wednesday, June 29, 2011 4:34 PM
    Wednesday, June 29, 2011 4:33 PM