This is the 1st of two blog posts that will show two techniques for displaying HTML email in an ASP page. This post will show how to integrate an email’s HTML body directly into an ASP page, whereas the next post will show how to display an email’s HTML body in an iframe (or frame).
Two new methods have been added to the Chilkat email object: AspUnpack and AspUnpack2. These methods write an HTML’s embedded images to a temp directory. The HTML’s IMG tags are updated to reference the temp files. AspUnpack also writes an HTML file to the temp directory (for the purpose of including it in an iframe tag). AspUnpack2 returns the modified HTML as utf-8 bytes. This post describes AspUnpack2 in detail:
prefix = "abc_"
saveDir = Server.MapPath("myEmailFiles")
relativeUrlPath = "myEmailFiles"
autoCleanFiles = 1
' AspUnpack2 returns the utf-8 HTML for the email.
Response.BinaryWrite email.AspUnpack2(prefix, saveDir, relativeUrlPath, autoCleanFiles)
AspUnpack2 has four arguments:
- prefix (String) — All files saved to the saveDir will have filenames that begin with this string. If autoCleanFiles = 1, then all files in saveDir beginning with prefix will be deleted before AspUnpack2 writes new files. This makes it easy to keep the saveDir directory uncluttered. The prefix provides a mechanism that allows separate sessions to view different emails simultaneously without colliding.
- saveDir (String) — Path of the temp directory on the web server where temporary files are written. Your code should either use an absolute path, or Server.MapPath. It should be a directory where the ASP worker process has permission to write and create files. Execute permissions and directory browsing permissions should be turned off.
- relativeUrlPath (String) — The relative URL path to be used in the IMG SRC attributes of the modified HTML. This should be the URL path from the ASP page to the files written to saveDir.
- autoCleanFiles (Long) — Set to 1 to delete all files in saveDir having filenames starting with prefix before saving new files.
The ASP code listing below is a simple example of displaying HTML email. Have a look at it, and then read these important notes about the code:
- The ASP page is utf-8 (i.e. the meta tag specifies utf-8). All emails, regardless of the charset used by the email, are output in utf-8. This sample ASP script is capable of displaying emails in all languages.
- This example saves the email’s attachments to a temp directory by calling SaveAllAttachments. It then iterates over the email’s attachments and creates a download link for each. If a temp directory is used for attachments, it should be setup with care — make sure execute and browse privileges are disabled, and make sure your web server runs anti-virus software that would immediately catch any virus file as it’s written.
- Setting the email’s Charset = "utf-8″ is important. It does not affect the email on the mail server. It does, however, force the output of AspUnpack2 to always use utf-8 — and that is required to match the charset of the ASP page.
- The HTML bytes returned by AspUnpack2 are utf-8 (because email.Charset = "utf-8″) and this data may be passed directly to Response.BinaryWrite.
- AspUnpack2 removes the HTML header as well as everything up to and including the opening tag. It also removes the closing body and html tags. The result is an HTML fragment that can be streamed directly into the result page.
- The handling of HTML emails that use CSS style sheets is still open-ended (for AspUnpack2) . The AspUnpack method saves the full HTML (with header) to a temporary HTML file, and this is displayed using a frame or iframe — and style sheet conflicts are not an issue.
- If an email is plain-text only, AspUnpack2 will HTML entity-encode the plain text (so that characters such as < are displayed correctly) and return the text enclosed in a <pre> block.
- This example is valid for emails on both POP3 and IMAP servers. The Chilkat Email (POP3/SMTP) and the Chilkat IMAP components share the same email object. Therefore, the AspUnpack and AspUnpack2 methods are available to both. </li>
ASP Source Listing - Display HTML Mail
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p>
<%
Set mailman = Server.CreateObject("Chilkat.MailMan")
mailman.UnlockComponent "Anything for 30-day trial."
mailman.MailHost = "mail.chilkatsoft.com"
mailman.PopUsername = "myLogin"
mailman.PopPassword = "myPassword"
‘ Download email into a bundle.
Set bundle = mailman.CopyMail()
if (Not (bundle Is Nothing)) then
if bundle.MessageCount > 0 then
‘ Get the 1st email.
Set email = bundle.GetEmail(0)
‘ Display the email.
Response.Write "Subject: " & email.Subject & "<br>"
Response.Write "From: " & email.From & "<br>"
Response.Write "Date: " & email.LocalDate & "<p>"
‘ Save attachments to a temporary directory and display download links.
success = email.SaveAllAttachments(Server.MapPath("data/attachments"))
if (success = 1) then
‘ Display download links to each attachment.
numAttach = email.NumAttachments
for i = 0 to numAttach - 1
fname = email.GetAttachmentFilename(i)
Response.Write "Attachment: <a href=""data/attachments/" & fname & _
""">" & fname & "</a><br>"
next
else
‘ Failed to save attachments, probably a permissions error…
Response.Write email.LastErrorHtml
end if
‘ Setting the email’s Charset to utf-8 will ensure that the email’s HTML body
‘ is utf-8, including the charset META tag.
email.Charset = "utf-8"
Response.Write "<p>"
‘ Display the email. The email may be plain-text or HTML –
‘ both kinds will display properly.
prefix = "abc_"
saveDir = Server.MapPath("data/htmlFiles")
urlPath = "data/htmlFiles"
cleanFiles = 1
‘ AspUnpack2 returns the utf-8 HTML for the email.
Response.BinaryWrite email.AspUnpack2(prefix,saveDir,urlPath,cleanFiles)
Response.Write "<p>"
Response.Write email.LastErrorHtml
else
Response.Write "No mail found."
end if
else
Response.Write mailman.LastErrorHtml
end if
%>
</body>
</html>