Sending E-mails via CDONTS
Sending E-mails via CDONTS
By Tom Vergote
30 March 2001
Sending an e-mail in ASP is not a difficult task, the only thing you need is CDONTS
(automatically installed with the SMTP service of IIS 4.0/5.0). You can also install a
free component on the server, such as ASPmail from
Server Objects
or SMTPmail from
Software Artisans (used on this site).
I'll show you how you can send mails via the CDONTS (CDO stands for Collaboration Data Objects, NTS for NT Server),
because this is available for everybody and the working method for a component is more or less the same.
First you have to create an instance of the CDONTS, like this:
<%
Option Explicit
Dim objCDOMail
Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
%>
The methods and properties of this object are straightforward, as the next code shows:
<%
objCDOMail.To = "someone@mysite.com" 'the destination
objCDOMail.From = "mailscript@mysite.com" 'the sender
objCDOMail.cc = "info@mysite.com" 'carbon copy
Dim txtBody
txtBody = "This email was send by an asp-script that uses CDONTS"
objCDOMail.Subject = "CDONTS" 'the subject
objCDOMail.Body = txtBody 'the content
objCDOMail.Send 'sending the email
%>
Until now everything is fairly simple, but there is more!
<%
objCDOMail.AttachFile("c:\wwwroot\mysite\mailscript.asp", "mailscript.asp")
objCDOMail.Bcc("secret@mysite.com")
objCDOMail.Importance = 1
%>
This is straightforward. The first line specifies an attachement, the second a blind carbon
copy and the last line the priority (if not stated = 1).
Priority:
0 -> low
1 -> normal
2 -> high
Attachment: ("path_to_the_file", "name")
The second parameter is optional and states the name that appears in the email (not necessarily
the real filename).
That's it, there's nothing more to it. Now you can finally write the script that
requests a confirmation after subscription to a mailinglist, or any other application.
It speaks for itself that this code can be extended to retrieve data from a database or form.
If you decide to install a component, I advise you to read the manual, but you'll definitely notice the principle is the same.
|