Crash Course in CDO, Part 3

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


A simple HTML email

So, we now know how to send a text email. But what about a HTML email? Well, it is almost as simple as a text email. I will also introduce another new thing here, Blind Carbon Copy (Bcc), and Carbon Copy (Cc). So, what are we waiting for, let’s write some code!


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


So, what did we change here? Actually not much. On line 9 and 10 we have the two lines related to sending a Bcc and a Cc. And line 11 is changed to HTMLBody, so we can send a HTML email.

But, what if we want to send a webpage, or a saved HTML file? Well, it is very simple, just change HTMLBody to CreateMHTMLBody. So, the code would look like:


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


The only new here is on line 9, where we are using the method CreateMHTMLBody to create the body of the email, which will now be a complete web site (or a complete file if that is what we chose to send).

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


Home | Copyright © 2002 - 2005 Kristofer Gäfvert