CDONTS发电子邮件示例

'Declare Variables
Dim CDONTSObj, MessageBody

'Create the CDONTS object
Set CDONTSObj = Server.CreateObject("CDONTS.NewMail")

'To Address
CDONTSObj.To = "info@scriptmate.com"

'From Address
CDONTSObj.From = "you@yoursite.com"

'CC Email Addresses seperated by a comma
CDONTSObj.cc = "support@scriptmate.com,amit@millioncolors.com"

'Your Email Message
MessageBody = "Sending a email using ASP was never so easy ! "

'Subject of the Email Message
CDONTSObj.Subject = "Send a Email"

'Assigning the value of your message to the Object
CDONTSObj.Body = MessageBody

'Send the email
CDONTSObj.Send

'Set the CDONTS object to nothing
Set CDONTSObj = nothing
.....................................................................................


<%
'Last Updated By Recon On 05/14/2001
'On Error Resume Next

'利用CDONTS组件在Win2k上发送邮件
Dim objMail

'普通邮件
'无主体
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Subject    = "TEST1"
objMail.Send
Set objMail = Nothing

'普通邮件
'有主体
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Subject    = "TEST2"
objMail.Body        = "When i was young, i listened to the radio ..."
objMail.Send
Set objMail = Nothing

'普通邮件
'有多送.有主体
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com;iamchn@263.NET"
objMail.Subject    = "TEST3"
objMail.Body        = "When i was young, i listened to the radio ..."
objMail.Send
Set objMail = Nothing

'普通邮件
'有抄送.有主体.有重要性
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Cc            = "iamchn@263.NET;recon_chan@sohu.com"
objMail.Subject    = "TEST4"
objMail.Body        = "When i was young, i listened to the radio ..."
objMail.Importance = 2
objMail.Send
Set objMail = Nothing

'普通邮件
'有抄送.有暗送.有主体.有重要性
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Cc            = "recon_chan@sohu.com"
objMail.Bcc             = "iamchn@263.NET;recon_chan@sina.com"
objMail.Subject    = "TEST5"
objMail.Body        = "When i was young, i listened to the radio ..."
objMail.Importance = 2
objMail.Send
Set objMail = Nothing

'普通邮件
'有主体.有重要性.含附件
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Subject    = "TEST6"
objMail.Body        = "When i was young, i listened to the radio ..."
objMail.Importance = 2

objMail.AttachFile "C:\Love.txt"
objMail.Send
Set objMail = Nothing

'HTML邮件
'有主体.有重要性
'注: AttachURL的使用可能有错误
Dim fso, tf
Dim strHTML

Set fso    = Server.CreateObject("SCRIPTING.FILESYSTEMOBJECT")
Set tf    = fso.OpenTextFile("C:\Mail.htm", 1)
strHTML = tf.ReadAll

Write strHTML
Set tf    = Nothing
Set fso    = Nothing

Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
objMail.From        = "iamchn@263.NET"
objMail.To            = "iamchn@21cn.com"
objMail.Subject    = "TEST7"
objMail.Body        = strHTML

objMail.BodyFormat    = 0
objMail.MailFormat    = 0
objMail.Importance    = 2

objMail.AttachURL "c:\common.CSS", "common.css"
objMail.AttachURL "c:\LOGO.gif", "logo.gif"
objMail.Send
Set objMail = Nothing
%>
.........................................................................................
<%
sub sendmail(fromwho,towho,subject,body)
dim cdo
set cdo=server.createobject("cdonts.newmail")
cdo.From=fromwho
cdo.To = towho
cdo.Subject =subject
cdo.Body =body
cdo.MailFormat=0
'说明是以MIME发送
cdo.BodyFormat=0
'可以包含HTML代码
cdo.Importance=0
'邮件的优先级别0-1-2
'cdo.cc=xx@xx.NET 抄送
'cdo.Bcc=xx@sf.NET密送
'cdo.AttachFile "" 附件
cdo.Send
Set cdo=Nothing
end sub
sub sendattachmail(fromwho,towho,subject,body,attachfile)
dim cdo
set cdo=server.createobject("cdonts.newmail")
cdo.From=fromwho
cdo.To = towho
cdo.Subject =subject
cdo.Body =body
cdo.MailFormat=0
cdo.BodyFormat=0
cdo.Importance=0
cdo.AttachFile attachfile
cdo.Send
Set cdo=Nothing
end sub
%
..............................................................................
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|
15|  
16|      Send email with CDO
17|  
18|  
19|      

20|        
21|            
22|              
23|              
24|            
25|            
26|              
27|              
28|            
29|            
30|              
31|              
32|            
33|            
34|              
35|              
37|            
38|            
39|              
41|            
42|        
Subject:
From:
To:
Message:
40|value="Send" />

43|      

44|  
45|

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|
18|  
19|      Send email with CDO
20|  
21|  
22|      

23|        
24|            
25|              
26|              
27|            
28|            
29|              
30|              
31|            
32|            
33|              
34|              
35|            
36|            
37|              
38|              
39|            
40|            
41|              
42|              
43|            
44|            
45|              
46|              
48|            
49|            
50|              
52|            
53|        
Subject:
From:
To:
Bcc:
Cc:
Message:
51|value="Send" />

54|      

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:

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.CreateMHTMLBody Request.Form("message")
10|   objMessage.Send
11|   Response.Redirect("Sent.HTML")
12|End If
13|%>
14|
15|
16|  
17|      Send email with CDO
18|  
19|  
20|      

21|        
22|            
23|              
24|              
25|            
26|            
27|              
28|              
29|            
30|            
31|              
32|              
33|            
34|            
35|              
36|              
37|            
38|            
39|              
41|            
42|        
Subject:
From:
To:
Message:
40|value="Send" />

43|      

44|  
45|


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).

Sending email using a remote server

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.

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.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|
23|  
24|      Send email with CDO
25|  
26|  
27|      

28|        
29|            
30|              
31|              
32|            
33|            
34|              
35|              
36|            
37|            
38|              
39|              
40|            
41|            
42|              
43|              
44|            
45|            
46|              
48|            
49|        
Subject:
From:
To:
Message:
47|value="Send" />

50|      

51|  
52|


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 14, 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 15 and 16, 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).

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.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|%>

It’s on line 17 the new stuff starts. First (line 17 and 18), 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 19 to 21 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 22 to 24. On line 25 and 26 we set that we will not use SSL for the communication to the SMTP server. And then on line 27 to 29, we set the timeout to 60 seconds.

Conclusion

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:


[本日志由 wang 于 2009-09-23 01:27 PM 编辑]
上一篇: 白河子成绩查询系统3.0
下一篇: 学校信息化系统的的需求分析
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1896
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭