5

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
 <[email protected]>; Mon, 16 Sep 2013 14:33:04 -0700
From: "[email protected]" <[email protected]>
To: My Name <[email protected]>
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: "[email protected]" <[email protected]>
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_--

4 Answers 4

8

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.
}
6
  • Can we use OpenPop.Net for attachment files also like .pdf , .xls , .doc & .txt ??? Nov 27, 2013 at 11:41
  • Yes, I was able to save attachments.
    – Robbert
    Nov 27, 2013 at 14:51
  • I got that we can use System.Net.Mail also . So which one is more easier Openpop or this one?? Nov 28, 2013 at 9:02
  • For my needs, Openpop was easier, but I didn't need to send messages, only retrieve messages for archiving.
    – Robbert
    Nov 28, 2013 at 16:23
  • 2
    @Shyam You can't parse email message using System.Net.Mail. Also using ASCII encoding may lead to data loss. Dec 5, 2013 at 13:38
2

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

0

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

0

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").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.