Of course. If you've got an existing HTML file you want to use you can simply
read it in from the file system. This allows you to use any HTML editor
to create the page and easily preview the message by simply viewing it with
a browser. The following code snippet illustrates how to use the FileSystemObject
to read in an HTML file and use it as the body of a message:
<%@ Language="VBScript" %>
<% Option Explicit
Dim objFSO, objFile
Dim objCDO
Dim strBody
' Read the HTML for the message body from a file on the
' server's hard drive.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Server.MapPath("template.htm"))
strBody = objFile.ReadAll()
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
' Send our message:
Set objCDO = Server.CreateObject("CDO.Message")
With objCDO
.From = "FROM_ADDRESS_GOES_HERE"
.To = "TO_ADDRESS_GOES_HERE"
.Subject = "Sample HTML Email (from File) from ASP 101!"
.HtmlBody = strBody
.Send
End With
Set objCDO = Nothing
%>
Here's the same thing in ASP.NET 2.0:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim objStreamReader As StreamReader
Dim strMessageBody As String
Dim strFrom, strTo, strSubject, strBody As String
Dim myMessage As MailMessage
Dim mySmtpClient As SmtpClient
' Read body of email from a file:
objStreamReader = File.OpenText(Server.MapPath("template.htm"))
strMessageBody = objStreamReader.ReadToEnd()
objStreamReader.Close()
' Send the email:
strFrom = "FROM ADDRESS GOES HERE <email@domain.com>"
strTo = "TO ADDRESS GOES HERE <email@domain.com>"
strSubject = "Sample HTML Email (from File) from ASP 101!"
strBody = strMessageBody
myMessage = New MailMessage(strFrom, strTo, strSubject, strBody)
myMessage.IsBodyHtml = True
mySmtpClient = New SmtpClient("localhost")
'mySmtpClient.Send(myMessage)
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Email (HTML from Template) Sample Page</title>
</head>
<body>
<p>
Please edit this page by entering the the appropriate
to and from email addresses before uncommenting the
Send method call.
</p>
</body>
</html>
If you want to use this approach and still personalize parts of the
message, you might consider incorporating the concepts outlined in
Using Template Files to Simplify Text Formatting in ASP.