频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

在win2003上用cdo代替CDONTS发邮件

来源: 作者: 出处:巧巧读书 2006-09-03 进入讨论组
QQRead:http://www.qqread.com/asp/2006/10/e241375.html

  Introduction

CDONTS was actually replaced by CDO already in Windows 2000 and Windows XP. But these Operating Systems supported CDONTS, and you could use CDONTS. Windows Server 2003 does not support CDONTS, and we are forced to use CDO. This tutorial is a crash course in CDO, and we will create a few simple web forms for sending emails with CDO and ASP.

Before we begin

Before I show you any code, there are a few things we must do to get this to work. The first thing is set up a web server – IIS 6.0. You can start the wizard for the installation of IIS 6.0 from Manage Your Server (Start->Programs->Administrative Tools->Manage Your Server).

Click Add or remove a role.
Select Application Server
You do not need ASP.NET nor FrontPage Server Extensions for this tutorial
But that is not enough; we also need the SMTP server to send our emails, though I will also show you how to use a remote server instead of the local. So, open up Add or Remove Programs from the Control Panel.

Click Add/Remove Windows Applications
Highlight Application Server, and click Details
Highlight Internet Information Services (IIS) and click Details
Select SMTP Service and click OK, and finally Next
We are almost ready for the code writing now, but since IIS 6.0 is locked down by default, we have to go to the Internet Information Services Manager (Start->Program->Administrative Tools) and enable the ASP extension.

When the IIS Manager has started, click on Web Service Extensions in the left pane.
Select Active Server Pages, and click on Allow

That’s it! We are now ready for the fun part, the coding!

A simple text email

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

01|<%
02|If Request.Form("btnSend").Count > 0 Then
03|
04|   Set objMessage = CreateObject("CDO.Message")
05|   objMessage.Subject = Request.Form("subject")
06|   objMessage.Sender = Request.Form("From")
07|   objMessage.To = Request.Form("To")
08|   objMessage.TextBody = Request.Form("message")
09|   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">
36|</textarea></td>
37|            </tr>
38|            <tr>
39|               <td colspan="2"><input type="submit" name="btnSend"
40|value="Send" /></td>
41|            </tr>
42|         </table>
43|      </form>
44|   </body>
45|</HTML>

And now some explanation to this code. On line 2 we make sure that the form has been submitted. 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!

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!

01|<%
02|
03|If Request.Form("btnSend").Count > 0 Then
04|
05|   Set objMessage = CreateObject("CDO.Message")
06|   objMessage.Subject = Request.Form("subject")
07|   objMessage.Sender = Request.Form("From")
08|   objMessage.To = Request.Form("To")
09|   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|&nb 更多文章 更多内容请看邮件服务器专题win2003频道专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章