Convert MIME to Plain-Text?
Question:
Is it possible to convert MIME to plain-text?
Answer:
The Chilkat MIME and/or Email API’s may be used to extract the plain-text body (or bodies) and/or HTML bodies from a MIME message. If a MIME-part is already plain-text, no conversion is necessary. It’s simply a matter of using the Chilkat MIME or Email API to extract the part’s contents. If the MIME-part is HTML, the Chilkat HTML-to-Text class/component may be used to convert the HTML to plain-text.
The following C# code extracts the HTML body from this MIME message and converts the HTML to plain-text:
MIME:
"MIME-Version: 1.0 Date: Wed, 23 Apr 2008 08:43:32 +0200 Message-ID: <CHILKAT-MID-848a7564-2f78-be1f-524c-5f7d96855306@shadow> X-Mailer: Chilkat Software Inc (http://www.chilkatsoft.com) X-Priority: 3 (Normal) Content-Type: multipart/alternative; boundary="-----_chilkat_6db_38cb_88bf414c.5fa87340_.ALT" This is a multi-part message in MIME format. -------_chilkat_6db_38cb_88bf414c.5fa87340_.ALT Content-Type: text/plain Content-Transfer-Encoding: 7bit -------_chilkat_6db_38cb_88bf414c.5fa87340_.ALT Content-Transfer-Encoding: quoted-printable Content-Type: text/html <html><head><META http-equiv=3D"Content-Type" content=3D"text/html;chars= et=3Dutf-8"></head><body><b><span style=3D'font-size:11pt;font-family:A= rial'><font color=3D#000000>Problembeschreibung:<br>... <br></body></html> -------_chilkat_6db_38cb_88bf414c.5fa87340_.ALT-- "
C# Code:
private void MimeToText()
{
Chilkat.Email email = new Chilkat.Email();
// Load a MIME document, which happens to be an email
// with an HTML body (in this case).
bool success = email.LoadEml("MimeToConvert.txt");
if (!success)
{
textBox1.Text = email.LastErrorText;
return;
}
Chilkat.HtmlToText h2t = new Chilkat.HtmlToText();
success = h2t.UnlockComponent("Anything for 30-dayt trial");
if (!success)
{
textBox1.Text = h2t.LastErrorText;
return;
}
// Get the email's HTML body:
string html = email.GetHtmlBody();
// Convert to plain-text:
string plainText = h2t.ToText(html);
textBox1.Text = plainText;
}
admin
0
Tags :