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

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
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()
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.

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

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

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