I get lots of emails on how to send email from an ASP page. Well there is lots of 3rd party components that you can buy. Which I've never boughten so I can't comment on them but I figure Why buy when IIS 4.0 comes with SMTP service. SMTP stands for Simple Mail Transfer Protocol, this is the agent that sends the mail to whatever address you give it.
This service is added for FREE!! Yes FREE! And comes with the CDONTS object. For the human person this object allows you to set some things up to send a simple email very painless and quickly. The demo below shows how quickly you can setup a basic input page and an ASP page to send the mail.
Some assumpsions before we get started:
Server running NT 4.0 w/service pak 3 or greater
IIS 4.0 w/SMTP service installed and setup to point to the SMTP Server.
For questions on how to exactly setup the SMTP service, check the NT Option pak 4.0 documentation-under Microsoft Internet Information Server. There is a whole section on the SMTP service as well others. This is how I figured out how to setup the service on the Server.(AND I ain't no ADMIN) or the standard consult your Network Admin!! This demo just helps you with the code that sends the mail. :)
Step 1: Create the Input Form:
'Create yourself and input form like this one below lets call this Mail_Input.htm
'Code for this page copy and paste if needed-Save you from coding. 'Easy thing to do is open a new notepad text document and copy & paste this into the new document 'Save As MailInput.htm to your web directory and poof new input document for FREE! <html> <head><title>Mail Input Page</title></head> <body> <form method="post" action="sendmail.asp" name="Inputform"> <table border="1" width="50%"> <tr><td width="48%">From</td> <td width="52%"> <input type="text" name="From" size="20"></td></tr> <tr><td width="48%">To</td><td width="52%"><input type="text" name="to" size="20"></td></tr> <tr><td width="48%">Subject</td><td width="52%"><input type="text" name="subject" size="20"></td></tr> <tr><td width="48%">Body</td><td width="52%"><input type="text" name="body" size="20"></td></tr> <tr><td width="48%"><input type="submit" value="Send" name="B1"><input type="reset"value="Reset" name="B2"></td> <td width="52%"> </td></tr> </table> </form> </body> </html>
Step 2: Create the ASP Page: sendmail.asp
Create a 2nd new page and save as sendmail.asp to your web directory. Below is the code that does the server side work to read the information and send the message.
<%
'Declare local variables to hold the data from the Input form page that is used above.
Dim strTo Dim strSubject Dim strBody 'Strings for recipient, subject, boby Dim objCDOMail 'The CDO object
'First we'll read in the values entered from the form into the Local variables strFrom = Request.Form("From") 'Make sure the From field has no spaces. strTo = Request.Form("to") strSubject = Request.Form("subject") strBody = Request.Form("body")
' Create an instance of the NewMail object. Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
' Set the properties of the object objCDOMail.From = StrFrom objCDOMail.To = strTo objCDOMail.Subject = strSubject objCDOMail.Body = strBody
' There are lots of other properties you can use. ' You can send HTML e-mail, attachments, etc... ' You can also modify most aspects of the message ' like importance, custom headers, ... ' Check the help files for a full list as well ' and the correct syntax.
' Some of the more useful ones I've included samples of here: 'objCDOMail.Cc = "mailto:sschofield@aspfree.com;steve@aspfree.com" Notice this sending to more than one person! 'objCDOMail.Bcc = "sschofield@aspfree.com;steve@aspfree.com" 'objCDOMail.Importance = 1 '(0=Low, 1=Normal, 2=High)im a 'objCDOMail.AttachFile "c:\path\filename.txt", "filename.txt"
' Send the message! objCDOMail.Send ' Set the object to nothing because it immediately becomes ' invalid after calling the Send method + it clears it out of the Server's Memory. Set objCDOMail = Nothing %> <html> <head><title>Sent Mail</title></head> <body> Your mail was sent to:<% = request("to") %><br> The time that is was sent was: <% = Now %>