繁体中文
设为首页
加入收藏
当前位置:ASP技术首页 >> ASP基础 >> 在win2003上用cdo代替CDONTS发邮件

在win2003上用cdo代替CDONTS发邮件

2006-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介: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 suppor...

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:

http://msdn.microsoft.com/library/default.ASP?url=/library/en-us/cdo/HTML/_olemsg_overview_of_cdo.ASP

责任编辑:admin
相关文章