CDOSYS用法及函数变量
CDOSYS全部用法,大量例子
CDOSYS是微软用来取代CDONTS组件的新组件,它是asp内置的发邮件组件,它的性能优于它的前辈--CDONTS.
CDOSYS使用更简便,性能更可靠 
CDOSYS的用法和属性:
- <%
- FieldBase = "http://schemas.microsoft.com/cdo/configuration/" '有些人会奇怪为什么会有URL出现,难道要链接到这个URL交换数据吗?其实不是的,这只是微软为这个字段的命名而已,你完全可以把它当作strName来看待而不是URL;至于微软为什么不用简短的名称而非要用这么长的名称,那你得去问问微软了.
- 注意:字段名称,包括下面的sendusing,sendusername等,全部小写,如果有大写出现则会出现"8004020A"错误,配置源中未找到smtp服务器
- Set Msg = CreateObject("CDO.Message")
- With Msg
- .Configuration.Fields.Item(FieldBase & "sendusing") = 2 '发送方式,必选
- .Configuration.Fields.Item(FieldBase & "sendusername") = "usr" '用户名
- .Configuration.Fields.Item(FieldBase & "sendpassword") = "pwd" '密码,如果不使用远程服务器,用户名和密码可以不要
- .Configuration.Fields.Item(FieldBase & "sendemailaddress") = "usr@server.com" '发送地址,可选
- .Configuration.Fields.Item(FieldBase & "smtpaccountname") = "usr@server.com" '帐户,可选
- .Configuration.Fields.Item(FieldBase & "smtpserver") = "smtp.server.com" '服务器域名,名称或IP,必选
- .Configuration.Fields.Item(FieldBase & "smtpserverport") = 25 '服务器端口,可选,默认为25
- .Configuration.Fields.Item(FieldBase & "smtpauthenticate") = 1 '服务器验证方式,0代表Anonymous(不验证),1代表Basic(使用basic(clear-text)验证),2代表NTLM(Secure Password Authentication in Microsoft Outlook Express),可选
- .Configuration.Fields.Item(FieldBase & "smtpconnectiontimeout") = 10 '服务器连接超时时间,可选
- .Configuration.Fields.Item(FieldBase & "smtpusessl") = true '服务器是否使用SSL安全链接,可选,如果设置了安全链接,不要忘记设置相应的端口
- .Configuration.Fields.Item(FieldBase & "languagecode") = 0x0804 '服务器使用的语言代码,这个我在使用的过程中会出现"未结束的字符串"的错误提示,而不使用这个参数也可以正常发送,可选
- .Configuration.Fields.Update '更新设置,使设置生效,必选
- .Subject = "Sending email with CDO" '邮件标题,必选
- .From = "usr@server.com" '发件人,必选,必须为设定服务器上真实有效的邮件地址
- .To = "usr1@server1.com" '收件人,多个邮件地址使用分号;隔开,必选
- .Bcc = "usr2@server2.com" '暗送,可选
- .Cc = "usr3@server3.com;usr4@server4.com" '抄送,可选
- .Importance = 2 '重要等级,可选
- .TextBody = "This is a message." '文本格式邮件内容
- .HTMLBody = "<h1>This is a message.</h1>" 'HTML格式邮件内容
- .CreateMHTMLBody "http://www.w3school.com.cn/asp/"
- .CreateMHTMLBody "file://c:/mydocuments/test.htm" '以一个指定的文件内容作为邮件内容发送,可以是URL,也可以是本地文件,但是要写好地址和路径
- '以上四者任选其一,可选
- .BodyPart.Charset = "gb2312"; '设置编码格式,必选
- .HTMLBodyPart.Charset = "gb2312"; '设置HTML编码格式,可选,如果发送内容为HTML格式,必选,否则使用CDOSYS发送时出现乱码
- .AddAttachment "c:\mydocuments\test.txt" '增加附件,可选
- .Send '发送,必选
- End With
- set =nothing
- %>
CDOSYS用法的例子:
这是我自己使用asp+CDOSYS发邮件的一段源代码,测试一切正常,没有任何错误
- FieldBase = "http://schemas.microsoft.com/cdo/configuration/"
- Set cdoCfg = CreateObject("CDO.Configuration")
- Set cdoMsg = CreateObject("CDO.Message")
- With cdoCfg.Fields
- .Item(FieldBase & "sendusing") = 2
- .Item(FieldBase & "sendusername") = "test"
- .Item(FieldBase & "sendpassword") = "passtest"
- '.Item(FieldBase & "sendemailaddress") = "test"
- '.Item(FieldBase & "smtpaccountname") = "smtp.126.com"
- .Item(FieldBase & "smtpserver") = "smtp.126.com"
- .Item(FieldBase & "smtpserverport") = 25
- .Item(FieldBase & "smtpauthenticate") = 1 '0代表Anonymous验证方式(不需要验证),1代表Basic验证方式(使用basic(clear-text)验证),2代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
- .Item(FieldBase & "smtpconnectiontimeout") = 10
- '.Item(FieldBase & "smtpusessl") = true
- '.Item(FieldBase & "languagecode") = 0x0804
- .Update
- End With
- With cdoMsg
- Set .Configuration = cdoCfg
- .From = "test@126.com"
- .To = "test1@live.com"
- .Subject = "这是CDOSYS测试"
- .HTMLBody = "<a href=http://www.netmkt.cn target=_blank>测试成功</a>"
- '.TextBody = "测试成功"
- .BodyPart.Charset = "gb2312"
- .HTMLBodyPart.Charset = "gb2312"
- .Send
- End With
- Set cdoCfg = Nothing
- Set cdoMsg = Nothing
注意:前面加'符号的可以删除
本人在测试这段代码的时候遇到很多错误,
比如使用 languagecode = 0x0804的时候出现"未结束的字符串"的错误提示,
还有其它诸如错误"8004020A"等的错误。 
以下例子为转载,网络上到处都是,不知道出处为何,正确性没有测试
使用 CDOSYS 发送电子邮件
CDO (Collaboration Data Objects) 是一项微软的技术,设计目的是用来简化通信程序的创建。 
CDOSYS 是 ASP 中的内置组件。我们将会您展示如何使用该组件来发送电子邮件。
CDONTs 怎么样?
微软已经在 Windows 2000、Windows XP 以及 Windows 2003 中淘汰了 CDONTs。如果您还在应用程序中使用 CDONTs,就需要更新代码,并使用新的 CDO 技术。 
使用 CDOSYS 的实例
发送电子邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .TextBody="This is a message."
- .Send
- set =nothing
- %>
使用 Bcc 和 CC 域来发送文本邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .Bcc="someoneelse@somedomain.com"
- .Cc="someoneelse2@somedomain.com"
- .TextBody="This is a message."
- .Send
- set =nothing
- %>
发送 HTML 邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .HTMLBody = "<h1>This is a message.</h1>"
- .Send
- set =nothing
- %>
发送一封由网站传送网页的 HTML 邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .CreateMHTMLBody "http://www.w3school.com.cn/asp/"
- .Send
- set =nothing
- %>
发送一封从您的电脑中的文件来传送网页的 HTML 邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .CreateMHTMLBody "file://c:/mydocuments/test.htm"
- .Send
- set =nothing
- %>
发送一封带有附件的电子邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .TextBody="This is a message."
- .AddAttachment "c:\mydocuments\test.txt"
- .Send
- set =nothing
- %>
使用远程服务器发送一封文本邮件:
- <%
- Set =CreateObject("CDO.Message")
- .Subject="Sending email with CDO"
- .From="@mydomain.com"
- .To="someone@somedomain.com"
- .TextBody="This is a message."
- .Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
- 'Name or IP of remote SMTP server
- .Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
- ="smtp.server.com"
- 'Server port
- .Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
- =25
- .Configuration.Fields.Update
- .Send
- set =nothing
- %>
Visual Basic代码
- CDO.IConfiguration iConfg = oMsg.Configuration;
- ADODB.Fields oFields = iConfg.Fields;
- oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;
- oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="myaccount@test.com"; //sender mail
- oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="myaccount@test.com"; //email account
- oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username";
- oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password";
- oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
- //value=0 代表Anonymous验证方式(不需要验证)
- //value=1 代表Basic验证方式(使用basic (clear-text) authentication.
- //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
- //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
- oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
- oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com";
- oFields.Update();
- Set cdoConfig = CreateObject("CDO.Configuration")
- With cdoConfig.Fields
- .Item(cdoSendUsingMethod) = cdoSendUsingPort
- .Item(cdoSMTPServer) = " <enter_mail.server_here>"
- .Item(cdoSMTPAuthenticate) = 1
- .Item(cdoSendUsername) = " <enter_username_here>"
- .Item(cdoSendPassword) = " <enter_password_here>"
- .Update
- End With
- Set cdoMessage = CreateObject("CDO.Message")
- With cdoMessage
- Set .Configuration = cdoConfig
- .From = "from@me.com"
- .To = "to@me.com"
- .Subject = "Sample CDO Message"
- .TextBody = "This is a test for CDO.message"
- .Send
- End With
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号