Active Server Pages Programming (ASP)/Using Forms To Email Info In ASP...
Expert: Srini Nagarajan - 6/7/2005
QuestionI plan to eventually migrate over to ASP.net, but am currently still using ASP for development purposes. I want to start creating forms that will allow a user to submit the info typed in the form boxes in a way that the info gets emailed to a specific email address. Is there a specific tag/s in ASP that will accomplish this goal that I need to look at, or are most people in the web development industry using some other type of option outside of ASP for this purpose? Basically my two options for building this type of application is to (1.) either collect user info into a DB table and export it into MS Excel for a report to supervisors, or (2.) allow the form submitted info to be emailed to a supervisor. Got any additional tips, suggestions?
AnswerHi
I would say use Email to Form which will be easier. When you move to ASP.NET then you put everything into the Database.
Here is the simple code to send email. This one uses Windows CDONTS object.
here is the steps
step 1:
Copy and paste the following HTML code into a file named CDONTSMail.HTM: (Customize the fields based on your requirement)
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR"
content="Microsoft FrontPage (Visual InterDev Edition) 2.0">
<title>CDONTSMail</title>
</head>
<body bgcolor="#FFFFFF">
<form action="CDONTSMail.asp" method="POST">
<table border="0">
<tr>
<td>From:</td>
<td><!--webbot bot="Validation"
b-value-required="TRUE" --><input type="text"
size="47" name="txtFrom"
value="Your internet address (Me@MyCompany.com)"></td>
</tr>
<tr>
<td>To:</td>
<td><!--webbot bot="Validation"
b-value-required="TRUE" --><input type="text"
size="47" name="txtTo"
value="The recipient's address (You@YourCompany.com)"></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" size="47" name="txtSubject"
value="Enter a subject here"></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="txtMessage" rows="9" cols="45">
Type your message here.</textarea></td>
</tr>
<tr>
<td valign="top">Importance:</td>
<td><input type="radio" name="optImportance"
value="2">High<br>
<input type="radio" checked name="optImportance"
value="1">Normal<br>
<input type="radio" name="optImportance" value="0">Low<br>
</td>
</tr>
</table>
<p><input type="submit" name="cmdSubmit" value="Submit">
<input type="reset" name="cmdClear" value="Clear"> </p>
</form>
</body>
</html>
Step 2:
Paste the following VBScript code into a file named CDONTSMail.ASP:
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type"
content="text/html;charset=iso-8859-1">
<TITLE>CDONTSMail</TITLE>
<%
Sub Write(strWriteThis)
'This subroutine just writes out whatever is
'passed to it.
response.write(strWriteThis & "<br>")
end sub
%>
</HEAD>
<BODY>
<%
Dim myCDONTSMail
Dim strFrom
Dim strTo
Dim strSubject
Dim strMessage
Dim lngImportance
'The following variable assignments are not required
'they are just here to make interpretation of the
'myCDONTSMail.Send line easier. You could put the
'Request.Form statements in the .Send line to cut down
'on the amount of code in the file.
strFrom=request.form("txtFrom")
strTo=request.form("txtTo")
strSubject = request.form("txtSubject")
strBody=request.form("txtMessage")
lngImportance = request.form("optImportance")
'The following four lines of code are just here for test
'purposes to see what variables have been pulled in from the
'HTM form.
Write("strFrom = " & strFrom)
Write("strTo = " & strTo)
Write("strSubject = " & strSubject)
Write("strMessage = " & strBody)
Write("Importance = " & lngImportance)
Set myCDONTSMail = CreateObject("CDONTS.NewMail")
myCDONTSMail.Send strFrom,strTo,strSubject,strBody,lngImportance
Set myCDONTSMail = Nothing
Write "Mail has been sent."
%>
</BODY>
</HTML>
NOTE: The NewMail object becomes invalid upon successful completion of the Send method, and you cannot reuse it for another message. You should Set it to Nothing to release the memory. Attempted access to a sent NewMail object results in a return of CdoE_INVALID_OBJECT.
STEP 3
Steps to Configure your Internet Information Server (IIS). In order to send mail from your IIS server via your SMTP server (assuming that they are different computers), complete the following steps:
a. On your IIS computer, open the Microsoft Management Console (MMC).
b. In the left pane, expand the "Internet Information Server" section.
c. In the left pane, select and expand your IIS server.
d. In the right pane, right-click "Default SMTP server" and select "Properties"
e. Select the "Delivery" tab.
f. In the "Fully Qualified Domain Name" text box, enter the IIS computer name.
g. In the "Smart Host" text box, enter the name of your SMTP server.
STEP 4.
Run the CDONTSMail.htm file in your browser, enter the required information, and then select the Submit button.
Happy Programming!
-Srini