Table of Contents
Introduction
Before we begin
A simple text email
A simple HTML email
Sending email using a remote server
Conclusion
Not always are we running a SMTP server on the same machine as the web server. But this is not a problem with CDO, and we can set it up so we are using a remote email server to send our emails. Let’s look at 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.TextBody = Request.Form("message")
10: objMessage.Configuration.Fields.Item _
11: ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
12: objMessage.Configuration.Fields.Item _
13: ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.101"
14: objMessage.Configuration.Fields.Item _
15: ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
16: objMessage.Configuration.Fields.Update
17: objMessage.Send
18: Response.Redirect("Sent.html")
19: End If
20: %>
21:
22: <html>
23: <head>
24: <title>Send email with CDO</title>
25: </head>
26: <body>
27: <form name="sendEmail" action="EmailWithCDO4.asp" method="post">
28: <table>
29: <tr>
30: <td>Subject:</td>
31: <td><input type="text" name="subject" /></td>
32: </tr>
33: <tr>
34: <td>From:</td>
35: <td><input type="text" name="from" /></td>
36: </tr>
37: <tr>
38: <td>To: </td>
39: <td><input type="text" name="to" /></td>
40: </tr>
41: <tr>
42: <td>Message: </td>
43: <td><input type="text" name="message"> </td>
44: </tr>
45: <tr>
46: <td colspan="2"><input type="submit" name="btnSend" value="Send" /></td>
47: </tr>
48: </table>
49: </form>
50: </body>
51: </html>
52:
53:

On line 10 and 11, we tell it to use a remote server to send the email with (the value 2). The default value is 1, which means that it will use the local SMTP service. On line 12 to 13, we specify the name of the server we will use to send the email (or in this case, the IP number of the server). And finally, on line 14 and 15, we set the port to use, which is port 25, the default for SMTP.
But, what if we have to logon to the remote server? Well, it isn’t more difficult than sending logon credentials in our application. This code sample shows this (the HTML code is the same as the above).
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.TextBody = Request.Form("message")
10: objMessage.Configuration.Fields.Item _
11: ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
12: objMessage.Configuration.Fields.Item _
13: ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.101"
14: objMessage.Configuration.Fields.Item _
15: ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
16: objMessage.Configuration.Fields.Item _
17: ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
18: objMessage.Configuration.Fields.Item _
19: ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sendEmailAccount"
20: objMessage.Configuration.Fields.Item _
21: ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mySecretPassword"
22: objMessage.Configuration.Fields.Item _
23: ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
24: objMessage.Configuration.Fields.Item _
25: ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
26:
27: objMessage.Configuration.Fields.Update
28: objMessage.Send
29: Response.Redirect("Sent.html")
30: End If
31: %>
32:
33: <html>
34: <head>
35: <title>Send email with CDO</title>
36: </head>
37: <body>
38: <form name="sendEmail" action="EmailWithCDO4.asp" method="post">
39: <table>
40: <tr>
41: <td>Subject:</td>
42: <td><input type="text" name="subject" /></td>
43: </tr>
44: <tr>
45: <td>From:</td>
46: <td><input type="text" name="from" /></td>
47: </tr>
48: <tr>
49: <td>To: </td>
50: <td><input type="text" name="to" /></td>
51: </tr>
52: <tr>
53: <td>Message: </td>
54: <td><input type="text" name="message"> </td>
55: </tr>
56: <tr>
57: <td colspan="2"><input type="submit" name="btnSend" value="Send" /></td>
58: </tr>
59: </table>
60: </form>
61: </body>
62: </html>
63:
64:
It’s on line 16 the new stuff starts. First (line 16 and 17), we set the authentication method. In this example we use Basic Authentication (clear-text). If we want to use NTLM (Windows Authentication), we would change the number to 2 instead of 1. Then, on line 18 to 19 we specify which user account we will use. I have a special account for this application called “sendEmailAccount”, which I used. We also have to pass the password to the SMTP server, and the password that will be sent is the one we set on line 20 to 21. On line 22 and 23 we set that we will not use SSL for the communication to the SMTP server. And then on line 24 to 25, we set the timeout to 60 seconds.
Using CDO is simple. Migrating from CDONTS to CDO should not be any problem, and as we have seen in this crash course, CDO is easy to learn. CDO is also very powerful, and can for example also be used to send news messages.
A complete reference can be found in MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_olemsg_overview_of_cdo.asp
[ 1 ] [ 2 ] [ 3 ] [ 4 ] [ Next ]