Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing email body with c#

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_--
like image 312
Robbert Avatar asked Dec 25 '22 20:12

Robbert


1 Answers

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.
}
like image 127
Robbert Avatar answered Dec 28 '22 08:12

Robbert