Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i'm a bit in a dead end here.
i have the following EML:

this is part of my eml file:
------=_NextPart_001_0046_01CD587D.979ABFF0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

aaaaaaaaaaa

------=_NextPart_001_0046_01CD587D.979ABFF0
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
...
...



as you can see, i have a body (which is HTML), and an alternate view (plain text).
outside plugin parses the entire EML into a MailMessage object.

the problem is, i have no idea what type is my body? (it can be html/plain/richtext/xml).

the MailMessage has isBodyHtml, which is fine to know if its html, but if its false - then it can be either plaintext/richtext/xml, and i need to know what is it

any help will be appreciated.
thanks!
Posted

As it comes from another party you can not rely on mime types told it.
Sniffing a data stream to determine it's content type is not easy. It relies on the file signature knowledge base, described here: http://www.garykessler.net/library/file_sigs.html[^]

There is a FindMimeFromData() function in the urlmon.dll, but that is unmanaged code, thus you need PInvoke, see: http://www.dotnet247.com/247reference/msgs/26/133278.aspx[^]. It has it's limitations, it can detect only 26 mime types. In linux there is an implementation called "magic numbers[^]".

This might be also interesting for you: A small Content Detection Library[^]
 
Share this answer
 
Comments
arielbeck 15-Aug-12 14:54pm    
Thanks!
i'll try that tomorrow, and see if it detects the body, when isBodyHtml=false, and there is no plaintext alternative in the AlternativeViews

i'll let u know
Zoltán Zörgő 15-Aug-12 15:25pm    
Actually what I gave you means to detect the type of an attachment, not the message body itself.
arielbeck 15-Aug-12 15:37pm    
oh :(...
then, back to the drawing board :(
You can detect whether it is html or plian text - that's right.
But Email does originally not support more foramts than HTML and plaintext.

SO what you can do is to treat each plaintext message like it's a richtext. That way, richtext is processed correctly and plaintext is also represented correctly.

Now to the XML:
That one is a bit hard to detect. How well formatted is your XML? is it in the bodytext? IN that case you should be able to find a in the first row of text.

Can't it be added as an attachment? Would be easier and safer for you to work with.
 
Share this answer
 
Comments
arielbeck 22-Aug-12 3:36am    
thanks for your answer :)
1. emails do support more than plain and html, (for example - in outlook u can create a rich one).
2. i will add what you said about richtext, tho right now the "viewer" supports only plain
3.i cannot add it to the attachment, because i receive the SMTP request, so i have no way to manipulate it when sending it, it can be anything.

it seems that there is no way to know, at least with .NET MailMessage, what is the body type in case of non-html.
TorstenH. 22-Aug-12 5:10am    
You should take a look at Thunderbird and how it works with attachments and different styles of body.
It tries to displays attachments after the original bodytext. SO if you have an attachment which is XML, you could just parse it and display the content (or whatever to do with it).

But to ask: What do you do with the email? display, send, receive, process/manipulate?
arielbeck 22-Aug-12 5:23am    
mostly parsing it, eventually, the body will be displayed in a viewer (which only supports plaintext right now).
TorstenH. 25-Aug-12 6:56am    
Then make it the thunderbird way.
I had a similar task quite a while ago where I had to display a certain type of report directly. SO I checked the attachments for a binary attachment and if the file extension was correct I started the rock'n'roll.
arielbeck 28-Aug-12 4:53am    
but i'm not handling attachments, i handle the body.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900