+2 votes

I have a problem when I read xml I get application/octet-stream in content type not text/xml in email attachment.

The file is embedded inside the email. I save the file using attachment.Save(@"c:\test\" + attachment.SafeFileName);.

The file saves without problem, but I need to read and process the file before save it to disk.

When I check the value of doc.LoadXml(mimeText.Text) I get null value.
How can I read the value for processing xml without saving to disk?

by

1 Answer

0 votes
 
Best answer

Mail.dll recognizes MIME entity type looking at its content-type header (MimeBase.ContentType).

If it is set to text/* (e.g.: text/plain, text/xml) or is not set at all, MimeText instance is created. Other classes are used for different content types (e.g. MimeCalendar, MimeVCard, MimeRfc822,...).

For 'application/octet-stream' MimeData is created.

Please note that a sender may use 'application/octet-stream', instead of more precise 'application/pdf'.

It's hard to tell from your questions what mimeText variable represents, but if you have MimeData you have its Data property and you can use it to access entity's bytes.

If somehow you know this entity contains XML data, you can easily create an XmlDocument:

IMail email = ...;
MimeData attachment = email.Attachments[0];
byte[] data = attachment.Data;

There are multiple ways to create XmlDocument from byte array:

XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data);
doc.LoadXml(xml);

-or-

XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream(data);
doc.Load(ms);

This assumes your data has UTF8 encoding which is the usual for XML.

by (300k points)
edited by
Excellent
XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream(data);
doc.Load(ms);
Works great.
Thank you
...