Question

I have a database table of email called email_archive. The table includes a field called body and another called raw_headers. I want to display the contents of this table on screen using C# (in a SharePoint webpart). I've been trying to find a library that can parse the body so that I can return the parts of the message to the window. I've tried a library from Limilabs as well as downloaded a couple of other libraries. However, all appear to require an email in EML format at the very minimal.

The latest attempt was trying to use MailUtilies.

MimeMessage mm = new MimeMessage(header + message);

But this is failing because it appears the format is not passing the MimeMessage integrity check.

Does anyone know of a way to parse an email into it's component parts using the raw headers and body content.

The headers look like this

MIME-Version: 1.0
Received: from server.domain.com (10.20.205.104) by
 mail.domain.com (xx.xx.xx.xx) with Microsoft SMTP Server id
 8.1.436.0; Mon, 16 Sep 2013 14:33:54 -0700
Received: from server (localhost.localdomain [127.0.0.1])   by
 server.domain.com (8.13.8/8.13.8) with ESMTP id r8GLX4vm007046 for
 <myaddress@domain.com>; Mon, 16 Sep 2013 14:33:04 -0700
From: "service@domain.com" <service@domain.com>
To: My Name <myaddress@domain.com>
Date: Mon, 16 Sep 2013 14:33:04 -0700
Subject: Some Topic
Thread-Topic: Some Topic
Thread-Index: Ac6zJHFgOvb7ZAdeTJC8DzqnAvdnOw==
Message-ID: <153372.442207427-sendEmail@gserver>
Reply-To: "service@domain.com" <service@domain.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 10
X-MS-Exchange-Organization-AuthSource: mail.domain.com
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Content-Type: multipart/alternative;
    boundary="_000_153372442207427sendEmailgroundwork_"

And the message looks something like this

--_000_153372442207427sendEmailgroundwork_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Some message to dispaly

--_000_153372442207427sendEmailgroundwork_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html><head><style type=3D'text/css'> p,h1 { font-family: arial; } 
</style></head><body>
 <p>Some message to display</p>
 </body></html>


--_000_153372442207427sendEmailgroundwork_--
Was it helpful?

Solution

I found the answer by using a library using OpenPop.Net.

public void addMessage(string message, string header) {
  string full_body = header + "\n" + message;
  System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  Byte[] full_body_bytes = encoding.GetBytes(full_body);
  Message mm = new Message(full_body_bytes);

  //do stuff here.
}

OTHER TIPS

I just used MimeKit and it worked great for parsing out attachments. It appears to be fully featured and cross-platform.

You need to combine your header and body with the \r\n\r\n delimiter. The following code demonstrates that:

string msgContent = header.TrimEnd("\r\n") + "\r\n\r\n" + message;
byte[] bytes = Encoding.ASCII.GetBytes(msgContent);
ComponentPro.Net.Mail.MailMessage msg = new ComponentPro.Net.Mail.MailMessage(bytes);

//
// Access your parsed message here
//

The code uses Ultimate Mail library

If you have headers and body, it's trivial to re-create entire eml:

string eml = header + "\r\n\r\n" + body;  

Headers and body in MIME format are separated by one empty line. The other answer uses '\n' - which is incorrect as MIME requires "\r\n" sequence as line ending.

If your header string already ends with new line, you just need to add a single new line ("\r\n").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top