Sending emails from asp by reading them from text files

Submitted on: 1/5/2015 1:26:00 PM
By: Bingo Solutions (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: ASP (Active Server Pages)
Views: 1404
     In this tutorial, I will share a good site function with you. I will show a good method which can be used to send emails to users. The tutorial will focus on sending e-mails to users(using ASP Email) by opening text files in which e-mail text is stored. At the end, I will present a good example of everything dicussed by building a "Forgot Login" application.

This article has accompanying files

Today, most sites send mails to users dynamically. This can be to alert users of a new post or send a user their login information. In this tutorial, I will share with you one of the best ways of incorporating this kind of feature into your ASP site. We will start by discussing the method and at the end I will present an example to you. The example will send login information to a user. If you are an advanced user or someone who does not like to read thru, just download the example files and read the setup instruction at the bottom of this page. 

The requirements for this tutorial are simple:

  • IIS or PWS
  • ASP Email(available for free at

    When we want our ASP pages to send an e-mail to a user, we have to have the e-mail content stored somewhere, usually in our ASP page. This is the method most people today use. They often store the e-mail message in a string like below:

    strMail="Welcome to," & username & Chr(10) 'Chr(10) is used to skip to the new line
    strMail= strMail & "Your order has been successfully queued!" & Chr(10)
    strMail=strMail & "Thank you for your business!"

    Although this is the most common method, it does not mean that it is the best method. Why? Because of one simple reason: it makes it diffcult for you to edit the text and also, it makes your code even dirtier. As a programmer, I always try to include as much code as I can in the ASP file and keep the rest for the include files. In the above example it might not look that bad, but when you get down to business and start sending those 100-lines e-mail to your users using this method, it simply will make your code look dirtier and dirtier.

    To fight this problem, I came up with a solution. I decided to use the FileSystemObject to open text files. Now be honest and you tell me, would you like to write an e-mail in an enviroment filled with complex ASP code or in a blank and white editor window of Notepad? Having the e-mail content in a text file can help things be more organized and makes changing the content much quicker and easier. In the example below, we will see how we can use this technique in a real-world scenerio.

    The example we are going to discuss will ask for the user's e-mail address and then e-mail the user with their username and password. The content of the e-mail will be read from the text file(ForgotLogin.txt). The application will consist of four total files(including database files and etc.). The files include:

  • ForgotPass1.asp- The file which will consist of basic a basic HTML form which would ask for the user's e-mail address.
  • ForgotPass2.asp- The file which will e-mails the user their username and password.
  • ForgotLogin.txt- Contain the text for the login information.
  • DB.MDB-A one-table database which will hold the username, password, and the e-mail address of members.


    As mentioned earlier, this file will contain the text message which will be e-mailed to the user. The content of this file should be:


    You had requested your login information. Well, here it goes:



    The Bakers Team

    I am sure you must be wondering what the purpose of {username} and {password} in the text file. I will tell you about that in a moment... so hang on!


    You can tell by its name that its an Access file. DB.MDB will have one table called tbl_Users. Below is the basic structure of tbl_Users:

    NOTE: You can download all files(including this) that are part of this project as a Zip by clicking here.


    ForgotPass1.asp is simply going to have an HTML form asking for the e-mail address of the user(as displayed below).

    Note that this file(if wanted) could be saved as .HTM but just for the sake of keeping things a ASP, I gave it a .ASP extension.


    ForgotPass2.asp is where most of the work is done. This page is responsible for retreiving the user login information from the database and then emailing it to the user. Below is the complete source code for this page. We will discuss portion-by-portion next. Note that you can find a link to download the whole project at the end of this tutorial.

     if Len(Email) > 0 Then 
     	strConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("DB.mdb")&";"
     	SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"
     	Dim objForgotLogin
     	Set objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB
     	objForgotLogin.Open SQL,strConnect
     		if objForgotLogin.EOF = False Then
     			'------------------Code For opening ForgotLogin.txt------------------------
     			Dim objFSO, objTextFile
     			Set objFSO = CreateObject("Scripting.FileSystemObject")
     			Set objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))
     			Do While Not objTextFile.AtEndOfStream
     			strTemp = strTemp & objTextFile.ReadLine & Chr(10)
     			'------------------End of code For opening ForgotLogin.txt
     			SendMail "Zaid","",strTemp,objForgotLogin("email"),"Your login information is enclosed!"
     			Response.write "<FONT face='Verdana' size=2>Your login information has been e-mailed To you!"
     			Set objFSO=Nothing
     			Set objTextFile=Nothing
     			Response.Write "<FONT face='Verdana' size=2 color='red'>Your e-mail address could Not be found!"
     		End if
     	Set objForgotLogin=Nothing
     	Response.Write "<FONT face='Verdana' size=2>Please enter an e-mail address</FONT>"
     End if
     'The function for sending the email using ASPEmail
     Function SendMail (FromName,FromEmail,MailContent,MailingAddress,Subject)
     Set Mail = Server.CreateObject("Persits.MailSender")
     Mail.Host = "" ' Specify a valid SMTP server
     Mail.From = FromEmail ' Specify sender's address
     Mail.FromName = FromName' Specify sender's name
     Mail.AddBcc MailingAddress
     Mail.Subject = subject
     Mail.Body = MailContent
     Set Mail=Nothing
     End Function

    Let us start off by quickly getting thru some of the basics. The first if statement checks to make sure that an e-mail address was atleast entered. If an e-mail address is not entered, an error message is displayed.

    If an e-mail address was entered, we have to check to make sure it exist in the database. For this, we have to build the SELECT statement like the one shown below:

    SQL="SELECT * FROM tbl_Users WHERE tbl_Users.Email='" & email & "'"

    Next, we use a recordset object to execute the above SQL query. Notice that strConnect holds the connection string for connecting to DB.MDB.

    Dim objForgotLogin
    Set objForgotLogin = Server.CreateObject("Adodb.Recordset") 'Used for pulling main categories from DB
    objForgotLogin.Open SQL,strConnect

    Once we have opened the SQL statement, we have to analyze the results. We start out by checking to see whether the EOF property of the recordset is true or false. If it is true, then it means that the user does not exist in the database and therefore displays an error. If its false, it means the user does exist and the application therefore proceeds.

    Once it has been made sure that the user exists, we can send the user their username and password. Now comes the part you must be waiting for the most: integrating the content of the text file with the e-mail message to be sent. Well, here is how we do it.

    First, look at the code below:

    Dim objFSO, objTextFile

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objTextFile = objFSO.OpenTextFile(Server.MapPath("ForgotLogin.txt"))

    Do While Not objTextFile.AtEndOfStream

    strTemp = strTemp & objTextFile.ReadLine & Chr(10)


    All this peice of code does is that it opens the text file which holds the contents of the e-mail and saves the file in a temporary string called strTemp. Do you remember that in the text file(forgotlogin.txt), we had text {username} and {password}? Well they were created as the hotspots which we can replace with the real username and password. We do this by using the simple Replace function of ASP/VBScript. What the code below does is that it first finds the word {username} in strTemp and then replaces it with the username the SQL query returned. The same is done for replacing the {password} with the "real" password.


    Well, belive it or not but once we have done this, all we have to do next is to send the e-mail to the user. We do this using the following code:

    SendMail "Zaid","",strTemp,objForgotLogin("email"),"Your login information is enclosed!"

    Notice that we specify strTemp as the content of the e-mail. Also, we get the user's e-mail address from the recordset(objForgotLogin("email")).

    That is really all there is to this example.

    NOTE:SendMail is a function I quickly developed to keep the function for e-mailing the user out of the main application code. I wont get into the details of it as it is very self-explanatory. You can goto if you want the details about ASPEmail.

    Setting up the applciation

    Setting up this example is very easy. Just follow the simple steps:

    1. Download the example.
    2. Unzip the example.
    3. Copy the folder named "Example" and paste it into the wwwroot directory.

    NOTE:You will have to open the MS Access database and add your e-mail address into tbl_Users before you can test it. Also, this database is for MS Access 2000 and if you have any problems, just re-make the Access file with the fields described in the DB.MDB section at the beginning of the tutorial. Also, you will need to change the connection string in ForgotPass2.asp.

    If you continue to have problems, feel free to contact me at
  • winzip iconDownload article

    Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
    1. Re-scan downloaded files using your personal virus checker before using it.
    2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

    If you don't have a virus scanner, you can get one at many places on the net

    Other 2 submission(s) by this author


    Report Bad Submission
    Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
    This submission should be removed because:

    Your Vote

    What do you think of this article (in the Intermediate category)?
    (The article with your highest vote will win this month's coding contest!)
    Excellent  Good  Average  Below Average  Poor (See voting log ...)

    Other User Comments

     There are no comments on this submission.

    Add Your Feedback
    Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

    To post feedback, first please login.