Lastest forums topics.
Mar 15 :: SQL SERVER CONNECTIVITY
CHECK DOMAIN AVAILABILITY
www.
STAY INFORMED
Enter your email address and receive our news and service announcements.

(Subscribe/UnSubscribe)

Sending emails with ASP and CDO

Sending emails from within ASP scripts is one of the simplest things you can do in ASP. To show the simplicity of email, we are going to use CDONTS.
Why ? CDONTS is included in IIS4 OP4 (Windows NT4) and in IIS5 (Windows 2000), therefore it's widely supported.

Although you can do a lot more with CDO than just send email, we're going to show only it's emailing capabilities. If you've ever used Outlook 97 - 2000, then you know that there's a lot more to it than just email (calendars, journals, agendas, directories, etc.)

Let's write some code :

<%
'You can send email in just two lines of code. Literaly!.
Set cdosmtp = Server.CreateObject("CDONTS.NewMail")
cdosmtp.Send("FromMe@mysite.com","ToYou@yoursite.com", _
             "Hello World!","Welcome to my website")
%>

The syntax for the send method above is as follows:
cdosmtp.send([From],[To],[Subject],[Body])

That of course was very simple, but it's not friendly, specially if your body and subject line are more than just a few characters long.
Here is a more friendly way of doing the same thing. It takes a little more coding, but it's easier to read and manage.

<%
Set cdosmtp = Server.CreateObject("CDONTS.NewMail")
cdosmtp.From = "FromMe@mysite.com"
cdosmtp.To = "ToYou@yoursite.com"
cdosmtp.Subject = "The Subject to your message"
cdosmtp.Body = "This is where you write the email message"
On Error Resume Next
cdosmtp.Send() 'Now we call the send method.
If Err Then Response.Write "Email failed!"
'Always clean up after your self. :)
Set cdosmtp = nothing
%>

There are other helpful properties to it such as:

cdosmtp.Importance = 1 'Low=0; Normal=1(Default); High=2
cdosmtp.AttachFile("e:\wwwroot\code\test.txt")
'This file MUST already be on the server.
cdosmtp.Cc = "someone@somewhere.com"
'You can also send carbon copies.
cdosmtp.Bcc = "someone@somewhere.com"
'and blind carbon copies also.

 

Send this article to a friend.

 

Posted : 11/18/2001