ASP Email
There are two methods of sending email using asp, with different server
components; CDONTS found on Windows 2000 servers, and CdoSys found on Windows
2003 servers.
Be aware that CDONTS is deprecated and may not be present on some Windows
2000 servers either, so you may need to use CdoSys.
Using CDONTS
<%
' read in the fields from your form, whatever they may be, for
example:
Dim strEmail, strMsg
strEmail = Request.Form("txtEmail")
strMsg = Request.Form("txtMsg")
Set myMail = CreateObject("CDONTS.NewMail")
myMail.From = "My Website<admin@mysite.com>"
myMail.To = strEmail
myMail.cc = ' if required
myMail.bcc = ' if required
myMail.Subject = "Subject"
myMail.Importance = 2 ' 0 = Low, 1 = Normal 2 = High
myMail.BodyFormat = 0 ' 0 = HTML 1 = Plain text
myMail.MailFormat = 0 'leave this out for Plain text
myMail.Body = strMsg
myMail.Send
set mymail = nothing
%>
Using CdoSys
First, you need to put a reference to the type library at the top of your
asp page in comment fields, as follows: (yes it refers to Windows 2000,
.......)
<!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->
Then read in the form fields you are sending in the email, configure the CDO
object and create the mail
<%
' read in the fields from your form, whatever they may be, for
example:
Dim strEmail, strMsg
strEmail = Request.Form("txtEmail")
strMsg = Request.Form("txtMsg")
' configure the CDO object
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "smtpmail"
.Update
End With
' create and send the email
Dim myMail
Set myMail = CreateObject("CDO.Message")
myMail.Configuration = cdoConfig
myMail.From = "My Website<admin@mysite.com>"
myMail.To = strEmail
myMail.To = ' if required
myMail.bcc = ' if required
myMail.Subject = "Subject"
myMail.HTMLBody = strMsg
myMail.Send
set myMail = nothing
set cdoConfig = nothing
%>
For a 'plain text' email, use myMail.Body, rather than myMail.HTMLBody.
Important
When sending an email from your site to yourself, say with details of an
enquiry, you may be tempted to use the enquirers email address as the 'From'
address. Don't!
Most servers now prevent sending email that is not from a domain on the
server, to stop the server being used to relay spam.
Use a know email address associated with your site, such as
admin@mysite.com.
|