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

December 11, 2006

Sending utf-8 Email in ASP

This post describes some pitfalls when trying to send utf-8 encoded email from classic ASP. The following ASP script has been saved from a text editor using the utf-8 character encoding. Can you spot the problem?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<%
	set mailman = Server.CreateObject("ChilkatMail2.ChilkatMailMan2")
	
	' Any string begins the 30-day trial
	success = mailman.UnlockComponent("30-day trial")
	if (success = 0) then
		Response.write "Unlock Failed!<br>"
	end if
	
	' Tell the mailman where the SMTP server is located
	mailman.SmtpHost = "smtp.comcast.net"
	
	'mailman.SmtpUsername = "myLogin"
	'mailman.SmtpPassword = "myPassword"
	
	' Create an Email message
	set email = Server.CreateObject("ChilkatMail2.ChilkatEmail2")
	
	' Enter the recipient's information
	email.AddTo "", "support@chilkatsoft.com"
	
	' Enter the sender's information
	email.FromName = ""
	email.FromAddress = "admin@chilkatsoft.com"
	
	' Enter the email subject
	email.Subject = "ABC Xyzezői Fórum"
	
	email.Body = "ABC Xyzezői Fórum"
	
	email.Charset = "utf-8"
	
	' Send Email from ASP
	if mailman.SendEmail(email) then
		Response.write mailman.LastErrorHtml
		Response.write "Message sent successfully!<br><br>"
		Response.End()
	else
		Response.write mailman.LastErrorHtml
		Response.End()
	end if
	
	Set email = Nothing
	Set mailman = Nothing
	
	%>
</body></html>

The problem is that you need to tell ASP that the .asp source file is utf-8. If you don’t each byte is interpreted as an ANSI character, which is likely to be Windows-1252, Windows-1250, etc. The utf-8 characters that require two bytes to encode a single character will instead be interpreted as two ANSI characters. To avoid this problem, tell ASP that your .asp source file is utf-8:

<% @CodePage = 65001 %>
<% Response.CodePage = 65001 %>

It is also wise to clearly specify the response code page so that it matches with the charset specified in the HTML metatag where utf-8 is indicated.

You may make this fix and still find that the email arrives and is displayed incorrectly: the utf-8 characters are displayed as two ANSI characters each. This can happen if you’re using an email client that is not capable of displaying utf-8. Eudora is one such email client — it is very poor in its ability to display international email. If you instead use an email client such as Mozilla Thunderbird, you can verify that the email is displayed correctly. In addition, you can do a "View Source" on the email with Mozilla Thunderbird and verify that the utf-8 characters are encoded correctly.

This leads to my last comment: I would avoid sending utf-8 email at this point in time. Too many email clients are lame and cannot handle it. If you instead omit the "email.Charset = " line, Chilkat will automatically detect the language and choose the appropriate ANSI character encoding for the email. You can also specifiy it explicitly. I would choose the charset that is the typical ANSI charset for a particular language (i.e. Shift_JIS for Japanese, Windows-1250 for Central European computers, Windows-1252 for Western European languages, etc.)


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.