Example code how to use IIS 4.0 new smtp service to send mail or email from asp pages.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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

From 
To
Subject
Body
 

'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%">&nbsp;<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%">&nbsp;</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!

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials