ASP 101 - Active Server Pages 101 - Web03
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
International

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



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES