An email message is simply a text file. In order for an SMTP server to send the file to its destination it must be placed in the SMTP server’s outbound queue directory. In this example we will create a text file that adheres to SMTP and save it in IIS outbound SMTP queue. This method is very simple to implement and doesn’t require an installation of a supporting component.
To begin create two files one file that contains the HTML form for entering in the email message, and then another page to contain the ASP code for creating the text file to drop in the SMTP pickup directory.
Below is the file that contains the HTML form code for passing our email information to the processing ASP script.
<html>
<head>
<title>Send Mail</title>
</head>
<body>
<form method="POST" action="sendmail.asp">
<pre>Send to Email: <input type="text" name="email" size="20">
Subject: <input type="text" name="subject" size="20"></pre>
<pre>Message:
<textarea rows="7" name="message" cols="42"></textarea></pre>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
Then create a new file in the same directory as the HTML form file called sendmail.asp. This file will be used for processing the form values and creating an email file for sending. The code entered into the sendmail.asp file is as follows:
<%
Dim strHTML
Sub CreateEmail
On Error Resume Next
Dim strMessage, strEmail, strSubject, tempMessage, MyEmailFile
Dim FSO, TS
'populate the variables with form values.
strMessage = Request.Form("message")
strEmail = Request.Form("email")
strSubject = Request.Form("subject")
'now build the message structure.
tempMessage = "x-sender: Patrick@Santry.com" & vbcrlf & _
"x-receiver: " & strEmail & vbcrlf & _
"From: Patrick@Santry.com" & vbcrlf & _
"To: " & strEmail & vbcrlf & _
"Subject: " & strSubject & vbcrlf & vbcrlf & _
strMessage
MyEmailFile = "c:\inetpub\mailroot\pickup\message.txt"
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set TS = FSO.CreateTextFile(MyEmailFile, True)
TS.Write tempMessage
TS.Close
Set FSO = Nothing
Set TS = Nothing
If Err <> 0 Then
strHTML = "An error occurred sending “ & _
“ this message: <br>" & vbcrlf & _
"Description: " & Err.Description
Else
strHTML = "Message was sent!"
End If
End Sub
CreateEmail
%>
<html>
<head>
<title>Message Status</title>
</head>
<body>
<%= strHTML %>
</body>
</html>
The sendmail.asp file contains one subroutine called CreateEmail. This subroutine accepts the values that were passed from the form file and uses them to create an SMTP compliant text file. Once the string for the contents of the file is built it then passed to the FileSystemObject in order to create the message file and save it into the pickup directory where the SMTP service will check periodically and send messages within it.
Note: You may want to name the file something other than message.txt if you plan on high traffic accessing this script. This way you won’t overwrite the file with a new message if the SMTP server didn’t pick up the message yet. Try naming the file based on time, or some other way of providing a unique name.
By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.