When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, April 30, 2000

Emailing Users their Forgotten Passwords, Part 2

By Eric Coffman

  • Read Part 1

  • In Part 1 we looked at creating the form where users could enter their email address to have their username/password emailed to them. In this part we will be looking at SendIt.asp, which is what actually does the work of emailing the user his or her username and password. First you need to make the database connection and declare your variables.

    - continued -

    <% Option Explicit %>
    Dim DATA_PATH, objDC, objRS, email, user, pass, sendmail
       'Maps to database.   Change to your database path.
       ' Create and establish data connection
    Set objDC = Server.CreateObject("ADODB.Connection")
    objDC.ConnectionTimeout = 15
    objDC.CommandTimeout = 30
    objDC.Open "DBQ=" & DATA_PATH & _
               ";Driver={Microsoft Access Driver (*.mdb)}; " & _
               "DriverId=25;MaxBufferSize=8192;Threads=20;", _
               "admin", "password"
    Set objRS = Server.CreateObject("ADODB.Recordset")
       'you may need to adjust this to suit your database
    objRS.Open "SELECT * FROM USERS WHERE email = '" & _
               email & "'", objDC, 0, 1

    Next we check to see if the email address is actually in the database.

    <title>Forgotten Password</title>
       'checks if email address exists in the database 
       'before sending a message.
    if objrs.EOF then
    <B><font color="red">
    There is no account for <%=email%>.
    <% Else %> 

    Then we set create a new CDONTS mail and define all the variables that the CDONTS component needs. You will need to set the sendmail.from value. Remember, we assume that there are fields in your database for email, username and, password.

       'sets variables
    email = request.form("email")
       'chooses username and password from database that correspond
       'to submitted email address.
    user = objrs.Fields("username")
    pass = objrs.Fields("password")
    Set sendmail = Server.CreateObject("CDONTS.NewMail")
       'put the webmaster address here
    sendmail.From = "webmaster@yoursite.com"
       'The mail is sent to the address entered in the previous page.
    sendmail.To = email
       'Enter the subject of your mail here
    sendmail.Subject = "The Login Information You Requested"
       'This is the content of thr message.
    sendmail.Body = "The login Information you requested." & _
                    vbCrlf & vbCrlf & "Username=" & user & _
                    vbCrlf & "Password=" & pass & vbCrlf
       'this sets mail priority.... 0=low 1=normal 2=high
    sendmail.Importance = 2
    sendmail.Send     'Send the email!

    Finally, we can show the user that a mail has been sent, and then close all the variables.

    Your login information has been mailed to <%=email%>.<br>
    You should receive it shortly.
       ' Close Data Access Objects and free DB variables
    	Set objRS =  Nothing
    	Set objDC = Nothing
    	Set sendmail = Nothing
    <%end if%>

    Well, this about wraps it up! Happy Programming!

  • Read Part 1


  • View the source for Forgot.asp in text format
  • View the source for SendIt.asp in text format

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article