[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
ASPFAQ_SendEmail
Back to ASP FAQ Main Page.

How to send email from a .ASP page


This is a example on how you can send email from a .ASP page using the CDONTS object.

This requires that the SMTP service is correctly installed and setup.

<%
'Send email using CDONTS and the SMTP server service
'(c) 2001 Programmers Heaven
'http://www.programmersheaven.com
Dim objCDO
'Create a new CDO objects
Set objCDO = Server.CreateObject("CDONTS.NewMail")
'Enter the sender's email address 
objCDO.To = "info@programmersheaven.com"
'Enter the recipients of this email, use ; to separate the addresses
objCDO.From = "info@programmersheaven.com"
'Enter the subject of this email
objCDO.Subject = "Testing sending Emails"
'Enter the body text of the message
objCDO.Body = "This is the content of the email" & vbcrlf
'Send the email!
objCDO.Send
'Release the object 
set objCDO=nothing
%>


Now to send mail in HTML format we need to set just a few extra properties of the CDONTS.NewMail object and, of course, create HTML body text, instead of plain text. We will also use the With command to make working with the properties of the NewMail object easier.

<%
Option Explicit
'Set up the body of the message
Dim strHTML
strHTML = "<html><head><title>E-Technik</title></head>"
strHTML = strHTML & "<body bgcolor=#FFFFFF>"
strHTML = strHTML & "Visit <a href=''http://www.e-technik.com'>E-Technik</a>"
strHTML = strHTML & "</body></html>"
'Set up the email object
Dim objMail
Set objMail = Server.CreateObject("CDONTS.NewMail")
With objMail
	.From = "webmaster@e-technik.com"
	.To = "someone@somewhere.com"
	.Subject = "E-Technik"
	.BodyFormat = 0 'CdoBodyFormatHTML
	.MailFormat = 0 'CdoMailFormatMime
	.Body = strHTML
	.Send
End With
Set objMail = Nothing
%>


The extra properties are BodyFormat and MailFormat which must both be set to 0 to send HTML mail. Notice that using the With command allows me to use the properties of the object without having to name the object every time.

Now, one of the most common uses for this is to create a contact form on your webpage that will send email directly from your site. To do this we will create 2 pages:
contact.asp - which contains the contact form
sendmail.asp - which sends the email.

First here is contact.asp:


<html>
 <head><title>Contact Me</title></head>
 <body>
 <%
   If Request("a") = "sent" Then
 %>
   Thank-you for your message.<br>
   <a href='contact.asp'>Send Another</a>
 <%
   Else
 %>
  Fill in this form to contact me:<br>
  <form action='sendmail.asp' method='post'>
    Your Email Address: <input type=text name=txtEmail><br>
    Subject: <input type=text name=txtSubject><br>
    Message:<br>
    <textarea name=txtMessage></textarea><br>
    <input type=submit>
  </form>
 <%
   End If
 %>
 </body>
</html>


Note: I have left out all kinds of formatting to keep it simple. Normally you would use a table for the form to make sure things line up nicely.

This page just contains a simple form where a visitor can fill in their email address, a subject and a message. That form data is submitted to sendmail.asp. If the Request object has a variable called "a" set to "sent", we thank the visitor for his mail, otherwise we draw the form.

Now for the sendmail.asp page.


<%
Option Explicit
'Set up the email object
Dim objMail
Set objMail = Server.CreateObject("CDONTS.NewMail")
With objMail
	.From = Request("txtEmail")
	.To = "you@yourdomain.com"
	.Subject = Request("txtSubject")
	.Body = Request("txtMessage")
	.Send
End With
Set objMail = Nothing
Request.Redirect "contact.asp?a=sent"
%>


This page is very similar to the code above, except that the from address, subject and message all come from the Request object, which contains the data filled in by the visitor on the contact.asp page. After the email has been sent, we redirect the page back to contact.asp, but with the "a=sent" query string so that the page thanks the user for his mail.

And there you have it, a simple contact form that sends mail directly from your site.


last edited (December 9, 2002) by KDivad Leahcim, Number of views: 9343, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.