Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

November 14, 2006

IE cannot load utf-8 MHT with utf-8 preamble

This problem has burned me more than once in the past: I have a .mht, I double-click on it to load it into Internet Explorer, but instead of displaying the web page it displays the MIME source. It turns out the IE can’t handle .mht files that begin with the utf-8 preamble. These are the three bytes at the beginning of a utf-8 text file (0xEF 0xBB 0xBF). Remove those three bytes and all is OK.

I most recently ran across this problem with this C# MHT example. It downloads a web page and creates the MHT in-memory, then attaches it to an email and zips it (all in-memory) and sends the email…

Chilkat.Mht mht = new Chilkat.Mht();
mht.UnlockComponent("Anything for 30-day trial");

Chilkat.MailMan mailman = new Chilkat.MailMan();
mailman.UnlockComponent("Anything for 30-day trial");

mailman.SmtpHost = "smtp.comcast.net";

string mhtStr = mht.GetMHT("http://www.google.com");

Chilkat.Email email = new Chilkat.Email();
email.Subject = "This is a test";
email.Body = "This is the email body";
email.From = "Matt ";
email.AddTo("Chilkat Support","support@chilkatsoft.com");

// Add the MHT as an attachment.
// Use windows-1252 here:
// The MIME string contains nothing more than us-ascii characters
// because it is encoded (either quoted-printable or base64).
// If you use utf-8, the string will include the 3-byte utf-8
// preamble, and if the recipient extracts the MHT from the zipped
// email attachment to a file and the tries to load it in Internet Explorer,
// the MIME source will display. This is a bug in IE. It cannot handle
// the utf-8 preamble. If the utf-8 preamble is removed, IE loads and
// displays the MHT correctly. Therefore, use windows-1252 here to
// prevent that problem…
email.AddStringAttachment2("google.mht",mhtStr,"windows-1252″);

// Convert all attachments to a single zipped attachment.
email.ZipAttachments("googleMht.zip");

bool success = mailman.SendEmail(email);
if (!success)
{
MessageBox.Show(mailman.LastErrorText);
}


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.