0

I am trying to read an email item attachment using EWS 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.

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • Does [this](http://social.msdn.microsoft.com/forums/en-US/0288deca-30bf-495e-a64e-25ef400d816d/ews-save-email-attachment-itemattachment) help? – groverboy Apr 14 '14 at 15:02

1 Answers1

1

Philip,

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.

Thanks,

--- Bob ---

Bob Bunn
  • 613
  • 3
  • 7
  • thanks Bob, I might need to save the item attachment to disk, so it can be loaded into our CRM system , but I cannot get the ItemType property to be recognized by .NET (using `ItemType TheItemAttachment = ((ItemAttachmentType) email.Attachments[0]).Item;` from [MSDN Blog: HOWTO: EWS: Use GetAttachment to download attachments off Mail](http://blogs.msdn.com/b/vikas/archive/2007/10/16/howto-ews-use-getattachment-to-download-attachments-off-mail-appointment.aspx) - do I need to do something else? – Our Man in Bananas Apr 14 '14 at 20:55
  • 1
    A couple of notes about that example. 1) It's not using EWS Managed API, it's using the generated proxy for the web service. This is not the recommended approach. 2) This example is not saving the email as a text file, it's saving as a .EML file from the MimeContent of the message. This is different from what you were describing as the desired outcome. You can check if the itemAttachment is an email message using something like: `if(itemAttachment.Item.ItemClass == "IPM.Note")` – Bob Bunn Apr 14 '14 at 21:25