locked
Exchange Web Services - convert emailitem attachment from Base64 string to Byte gives error RRS feed

  • Question

  • I am trying to read an email item attachment using EWS (not a file atatchment, I mean an email that has been attached) and save it to disk as a text file so it can be used later on.

    I am getting an error:

    "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. "

    here is my code:

    Directory.CreateDirectory(emailAttachmentsPath);
    
    // Put attachment contents into a stream. C:\Dev\EWSHelloWorld
    emailAttachmentsPath = emailAttachmentsPath + "\\" + sEmailSubject+".txt";
    
    //save to disk 
    using (Stream FileToDisk = new FileStream(emailAttachmentsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        byte[] ContentBytes = System.Convert.FromBase64String(itemAttachment.ToString());
    
        FileToDisk.Write(ContentBytes, 0,ContentBytes.Length);
        FileToDisk.Flush();
        FileToDisk.Close();
    } 

    what is the best way to do this please?

    I basically want the text of the email in a text file, and any attachments in that email would be saved to disk as well (I can do that part I think using FileStream

    Monday, April 14, 2014 2:59 PM

Answers

  • There is no Base64 conversion required to save as an EML file. In order to save as an EML file you need to first load the MimeContent:

    itemAttachment.Load(new PropertySet(ItemSchema.MimeContent));

    Now you can save the content to an EML file with something like this:

    File.WriteAllBytes("Email.eml", itemAttachment.Item.MimeContent.Content);


    --- Bob --- Microsoft Exchange Developer Documentation

    Wednesday, April 16, 2014 2:29 PM

All replies

  • You will not be able to use the Convert() method on an ItemAttachment because it is not Base64 encoded. The item attachment has a lot of properties about the item, and if I understand your request properly you are looking for just the body of the email.

    The first thing you will want to consider is adding a check to see if the ItemAttachment is an email message. If it is, there are couple of lines to get to the text of the email body:

    itemAttachment.Load(

    new

    PropertySet(BasePropertySet.FirstClassProperties)

    ); string BodyText = itemAttachment.Item.Body.Text;

    The first line will load the item and it's first class properties. The second line will get the text version of the email body.

    I hope this helps. If this does resolve your question, please mark the post as answered.


    --- Bob --- Microsoft Exchange Developer Documentation


    • Edited by Bob Bunn Monday, April 14, 2014 8:06 PM Syntax error
    Monday, April 14, 2014 8:04 PM
  • Thanks Bob,

    One more question, what do I need to do in order to save the item as an mht file or eml file on disk?I have tried many things, but am missing something somewherehere is what I tried, but it fails with an error about content type or mime type

        string itemAttachmentName = itemAttachment.Name.Replace(":", "");

        emailAttachmentsPath = emailAttachmentsPath + "\\" + emailDisplayTo + "."
            + emailSenderName + "." + System.DateTime.Now.ToString("HHmmss");

        Directory.CreateDirectory(emailAttachmentsPath);

        //// Put attachment contents into a stream. C:\Dev\EWSHelloWorld
        emailAttachmentsPath = emailAttachmentsPath + "\\" + itemAttachmentName + ".txt";

        //save to disk
        using (Stream FileToDisk = new FileStream(emailAttachmentsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
        byte[] ContentBytes = System.Convert.FromBase64String(itemAttachment.ContentType);

        FileToDisk.Write(ContentBytes, 0, ContentBytes.Length);
        FileToDisk.Flush();
        FileToDisk.Close();
    }

    if I cannot use the conversion from Base64 because as you say above, it is not Base64 encoded, how should save this to disk?

    Tuesday, April 15, 2014 4:15 PM
  • There is no Base64 conversion required to save as an EML file. In order to save as an EML file you need to first load the MimeContent:

    itemAttachment.Load(new PropertySet(ItemSchema.MimeContent));

    Now you can save the content to an EML file with something like this:

    File.WriteAllBytes("Email.eml", itemAttachment.Item.MimeContent.Content);


    --- Bob --- Microsoft Exchange Developer Documentation

    Wednesday, April 16, 2014 2:29 PM
  • thanks Bob, how is it affected if the item attachment itself has attachments? Will they be written in the email file or do I need to load further properties for them?
    Thursday, April 17, 2014 9:19 AM
  • Your best bet is to enumerate through each attachment and see if they have attachments and save those as appropriate. Saving with MimeContent is no guarantee that all properties will be saved as you saw it in Exchange.

    --- Bob --- Microsoft Exchange Developer Documentation

    Thursday, April 17, 2014 2:32 PM