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 %> </body> </html> Good luck!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
| | | | Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |