ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

FAQ for the E-mail (HTML) sample:

Is there a way to load my HTML from a file so I don't have to deal with things like double quotes and vbCrLf characters?

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()
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
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()

        ' Send the email:
        strFrom    = "FROM ADDRESS GOES HERE <>"
        strTo      = "TO ADDRESS GOES HERE <>"
        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")
    End Sub

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <title>Email (HTML from Template) Sample Page</title>

Please edit this page by entering the the appropriate
to and from email addresses before uncommenting the
Send method call.


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.

Was your question not answered above?

Ask a new question

Please note: This form is only for submitting questions about the sample for us to consider including in the FAQ. If we feel the question merits inclusion, we will include it along with a reply. We will not respond to your email individually.

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers