This is the 2nd of two posts about displaying HTML email in ASP. The 1st article is located here: Display HTML Email in ASP.
The 1st article shows how to embed an email’s HTML body directly within an ASP page. This article shows how to display an email’s HTML body within an iframe or frame.
Before beginning, here’s the full ASP example:
<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
' Convert this email to utf-8:
email.Charset = "utf-8"
Response.Write "<p>"
' Display the email. The email may be plain-text or HTML.
' Do this by unpacking the email to a sub-directory and displaying
' the HTML in an iframe.
' All temp files will have filenames beginning with prefix:
prefix = "abc_"
saveDir = Server.MapPath("data/htmlFiles")
urlPath = "data/htmlFiles"
autoCleanFiles = 1
success = email.AspUnpack(prefix, saveDir, urlPath, autoCleanFiles)
if (success = 1) then
Response.Write "<iframe width=""100%"" height=""100%"""
Response.Write " scrolling=auto frameborder=0 src=""data/htmlFiles/abc_Email.html"" ></iframe>"
else
Response.Write email.LastErrorHtml
end if
Response.Write "<p>"
Response.Write email.LastErrorHtml
else
Response.Write "No mail found."
end if
else
Response.Write mailman.LastErrorHtml
end if
%>
</body>
</html>
How it works:
The AspUnpack method writes the email’s HTML body to an HTML file in a temp directory. It also writes any embedded images, style sheets, etc. to the same directory. The URLs in the HTML are modified to reference the files written to the temp directory. The HTML file may then be referenced as the source for a frame or iframe.
Most of the details of AspUnpack2 apply to AspUnpack. Please refer to Display HTML Email in ASP for more information.