Crash Course in CDO, Part 2

Table of Contents
Introduction
Before we begin
A simple text email
A simple HTML email
Sending email using a remote server
Conclusion


A simple text email

So, start your favorite text editor, and type this:


     1: <%
     2: If Request.Form("btnSend").Count > 0 Then
     3: 
     4:    Set objMessage = CreateObject("CDO.Message")
     5:    objMessage.Subject = Request.Form("subject")
     6:    objMessage.Sender = Request.Form("From")
     7:    objMessage.To = Request.Form("To")
     8:    objMessage.TextBody = Request.Form("message")
     9:    objMessage.Send
    10:    Response.Redirect("Sent.html")
    11: End If
    12: %>
    13: 
    14: <html>
    15:    <head>
    16:       <title>Send email with CDO</title>
    17:    </head>
    18:    <body>
    19:       <form name="sendEmail" action="EmailWithCDO.asp" method="post">
    20:          <table>
    21:             <tr>
    22:                <td>Subject:</td>
    23:                <td><input type="text" name="subject" /></td>
    24:             </tr>
    25:             <tr>
    26:                <td>From:</td>
    27:                <td><input type="text" name="from" /></td>
    28:             </tr>
    29:             <tr>
    30:                <td>To: </td>
    31:                <td><input type="text" name="to" /></td>
    32:             </tr>
    33:             <tr>
    34:                <td valign="top">Message: </td>
    35:                <td><textarea name="message" rows="6" cols="30"></textarea></td>
    36:             </tr>
    37:             <tr>
    38:                <td colspan="2"><input type="submit" name="btnSend" value="Send" /></td>
    39:             </tr>
    40:          </table>
    41:       </form>
    42:    </body>
    43: </html>
    44: 
    45: 


And now some explanation to this code. On line 2 we make sure that the form has been submitted (not the first time the visitor sees it, but when he/she has clicked the Send button) . If it has, the count for the send button will be greater than 0. Line 4 creates a reference to the CDO component. Then, on line 5 to 8, we fill in subject, sender, receiver, and the text body. We do not fill out the From Property. The difference between From and Sender is that Sender identifies the user that actually submits the message, but From on the other hand, designates the author(s). Line 9 sends the email, and on line 10, we redirect the user to another page, with a message that the email has been sent. The rest of the code is pure HTML, and this tutorial assumes you have this knowledge.

So, browse the file, fill in the form, and click Send. Wait a while, and soon you will receive an email (you did change the email address to you own, didn’t you?).

This was a simple example of using CDO. And yes, it is this simple!

[ 1 ] [ 2 ] [ 3 ] [ 4 ] [Next ]


Home | Copyright © 2002 - 2005 Kristofer Gäfvert