追求永无止尽

-- Ж飞羽忘寒Ж

导航

CDOSYS用法及函数变量

CDOSYS全部用法,大量例子

CDOSYS是微软用来取代CDONTS组件的新组件,它是asp内置的发邮件组件,它的性能优于它的前辈--CDONTS.
CDOSYS使用更简便,性能更可靠

CDOSYS的用法和属性:

  1. <%   
  2. FieldBase = "http://schemas.microsoft.com/cdo/configuration/" '有些人会奇怪为什么会有URL出现,难道要链接到这个URL交换数据吗?其实不是的,这只是微软为这个字段的命名而已,你完全可以把它当作strName来看待而不是URL;至于微软为什么不用简短的名称而非要用这么长的名称,那你得去问问微软了.
  3. 注意:字段名称,包括下面的sendusing,sendusername等,全部小写,如果有大写出现则会出现"8004020A"错误,配置源中未找到smtp服务器   
  4. Set Msg = CreateObject("CDO.Message")   
  5. With Msg   
  6.    .Configuration.Fields.Item(FieldBase & "sendusing") = 2   '发送方式,必选
  7.    .Configuration.Fields.Item(FieldBase & "sendusername") = "usr" '用户名
  8.    .Configuration.Fields.Item(FieldBase & "sendpassword") = "pwd" '密码,如果不使用远程服务器,用户名和密码可以不要
  9.    .Configuration.Fields.Item(FieldBase & "sendemailaddress") = "usr@server.com" '发送地址,可选
  10.    .Configuration.Fields.Item(FieldBase & "smtpaccountname") = "usr@server.com" '帐户,可选
  11.    .Configuration.Fields.Item(FieldBase & "smtpserver") = "smtp.server.com" '服务器域名,名称或IP,必选
  12.    .Configuration.Fields.Item(FieldBase & "smtpserverport") = 25   '服务器端口,可选,默认为25
  13.    .Configuration.Fields.Item(FieldBase & "smtpauthenticate") = 1  '服务器验证方式,0代表Anonymous(不验证),1代表Basic(使用basic(clear-text)验证),2代表NTLM(Secure Password Authentication in Microsoft Outlook Express),可选
  14.    .Configuration.Fields.Item(FieldBase & "smtpconnectiontimeout") = 10  '服务器连接超时时间,可选
  15.    .Configuration.Fields.Item(FieldBase & "smtpusessl") = true   '服务器是否使用SSL安全链接,可选,如果设置了安全链接,不要忘记设置相应的端口
  16.    .Configuration.Fields.Item(FieldBase & "languagecode") = 0x0804  '服务器使用的语言代码,这个我在使用的过程中会出现"未结束的字符串"的错误提示,而不使用这个参数也可以正常发送,可选
  17.    .Configuration.Fields.Update       '更新设置,使设置生效,必选
  18.    .Subject = "Sending email with CDO" '邮件标题,必选
  19.    .From = "usr@server.com" '发件人,必选,必须为设定服务器上真实有效的邮件地址
  20.    .To = "usr1@server1.com" '收件人,多个邮件地址使用分号;隔开,必选
  21.    .Bcc = "usr2@server2.com" '暗送,可选
  22.    .Cc = "usr3@server3.com;usr4@server4.com" '抄送,可选
  23.    .Importance = 2      '重要等级,可选
  24.    .TextBody = "This is a message." '文本格式邮件内容
  25.    .HTMLBody = "<h1>This is a message.</h1>" 'HTML格式邮件内容
  26.    .CreateMHTMLBody "http://www.w3school.com.cn/asp/"
  27.    .CreateMHTMLBody "file://c:/mydocuments/test.htm" '以一个指定的文件内容作为邮件内容发送,可以是URL,也可以是本地文件,但是要写好地址和路径
  28. '以上四者任选其一,可选
  29.    .BodyPart.Charset = "gb2312";     '设置编码格式,必选
  30.    .HTMLBodyPart.Charset = "gb2312";   '设置HTML编码格式,可选,如果发送内容为HTML格式,必选,否则使用CDOSYS发送时出现乱码
  31.    .AddAttachment "c:\mydocuments\test.txt" '增加附件,可选
  32.    .Send       '发送,必选
  33. End With
  34. set =nothing   
  35. %>  

CDOSYS用法的例子:

这是我自己使用asp+CDOSYS发邮件的一段源代码,测试一切正常,没有任何错误

  1. FieldBase = "http://schemas.microsoft.com/cdo/configuration/"
  2. Set cdoCfg = CreateObject("CDO.Configuration")   
  3. Set cdoMsg = CreateObject("CDO.Message")   
  4. With cdoCfg.Fields   
  5.      .Item(FieldBase & "sendusing") = 2   
  6.      .Item(FieldBase & "sendusername") = "test"
  7.      .Item(FieldBase & "sendpassword") = "passtest"
  8. '.Item(FieldBase & "sendemailaddress") = "test"
  9. '.Item(FieldBase & "smtpaccountname") = "smtp.126.com"
  10.      .Item(FieldBase & "smtpserver") = "smtp.126.com"
  11.      .Item(FieldBase & "smtpserverport") = 25   
  12.      .Item(FieldBase & "smtpauthenticate") = 1 '0代表Anonymous验证方式(不需要验证),1代表Basic验证方式(使用basic(clear-text)验证),2代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)  
  13.      .Item(FieldBase & "smtpconnectiontimeout") = 10   
  14. '.Item(FieldBase & "smtpusessl") = true
  15. '.Item(FieldBase & "languagecode") = 0x0804
  16.      .Update   
  17. End With
  18. With cdoMsg   
  19. Set .Configuration = cdoCfg   
  20.      .From = "test@126.com"
  21.      .To = "test1@live.com"
  22.      .Subject = "这是CDOSYS测试"
  23.      .HTMLBody = "<a href=http://www.netmkt.cn target=_blank>测试成功</a>"
  24. '.TextBody = "测试成功"
  25.     .BodyPart.Charset = "gb2312"
  26.     .HTMLBodyPart.Charset = "gb2312"
  27.     .Send   
  28. End With
  29. Set cdoCfg = Nothing
  30. Set cdoMsg = Nothing

注意:前面加'符号的可以删除
本人在测试这段代码的时候遇到很多错误,
比如使用 languagecode = 0x0804的时候出现"未结束的字符串"的错误提示,
还有其它诸如错误"8004020A"等的错误。

以下例子为转载,网络上到处都是,不知道出处为何,正确性没有测试

使用 CDOSYS 发送电子邮件
CDO (Collaboration Data Objects) 是一项微软的技术,设计目的是用来简化通信程序的创建。

CDOSYS 是 ASP 中的内置组件。我们将会您展示如何使用该组件来发送电子邮件。

CDONTs 怎么样?
微软已经在 Windows 2000、Windows XP 以及 Windows 2003 中淘汰了 CDONTs。如果您还在应用程序中使用 CDONTs,就需要更新代码,并使用新的 CDO 技术。

使用 CDOSYS 的实例

发送电子邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .TextBody="This is a message."
  7. .Send   
  8. set =nothing   
  9. %> 

使用 Bcc 和 CC 域来发送文本邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .Bcc="someoneelse@somedomain.com"
  7. .Cc="someoneelse2@somedomain.com"
  8. .TextBody="This is a message."
  9. .Send   
  10. set =nothing   
  11. %> 

发送 HTML 邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .HTMLBody = "<h1>This is a message.</h1>"
  7. .Send   
  8. set =nothing   
  9. %>   

发送一封由网站传送网页的 HTML 邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .CreateMHTMLBody "http://www.w3school.com.cn/asp/"
  7. .Send   
  8. set =nothing   
  9. %> 

发送一封从您的电脑中的文件来传送网页的 HTML 邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .CreateMHTMLBody "file://c:/mydocuments/test.htm"
  7. .Send   
  8. set =nothing   
  9. %> 

发送一封带有附件的电子邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .TextBody="This is a message."
  7. .AddAttachment "c:\mydocuments\test.txt"
  8. .Send   
  9. set =nothing   
  10. %> 

使用远程服务器发送一封文本邮件:

  1. <%   
  2. Set =CreateObject("CDO.Message")   
  3. .Subject="Sending email with CDO"
  4. .From="@mydomain.com"
  5. .To="someone@somedomain.com"
  6. .TextBody="This is a message."
  7. .Configuration.Fields.Item _   
  8. ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2   
  9. 'Name or IP of remote SMTP server
  10. .Configuration.Fields.Item _   
  11. ("http://schemas.microsoft.com/cdo/configuration/smtpserver") _   
  12. ="smtp.server.com"
  13. 'Server port
  14. .Configuration.Fields.Item _   
  15. ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _   
  16. =25   
  17. .Configuration.Fields.Update   
  18. .Send   
  19. set =nothing   
  20. %> 

Visual Basic代码

  1.    CDO.IConfiguration   iConfg   =   oMsg.Configuration;     
  2.     ADODB.Fields   oFields   =   iConfg.Fields;     
  3. oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;     
  4. oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="myaccount@test.com";   //sender   mail     
  5. oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="myaccount@test.com";   //email   account     
  6. oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username";     
  7. oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password";       
  8. oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;     
  9. //value=0   代表Anonymous验证方式(不需要验证)     
  10. //value=1   代表Basic验证方式(使用basic   (clear-text)   authentication.       
  11. //The   configuration   sendusername/sendpassword   or   postusername/postpassword   fields   are   used   to   specify   credentials.)     
  12. //Value=2   代表NTLM验证方式(Secure   Password   Authentication   in   Microsoft   Outlook   Express)     
  13. oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;     
  14. oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.21cn.com";     
  15.      oFields.Update();     
  16. Set cdoConfig = CreateObject("CDO.Configuration")     
  17. With cdoConfig.Fields     
  18.          .Item(cdoSendUsingMethod) = cdoSendUsingPort     
  19.          .Item(cdoSMTPServer) = " <enter_mail.server_here>"
  20.          .Item(cdoSMTPAuthenticate) = 1     
  21.          .Item(cdoSendUsername) = " <enter_username_here>"
  22.          .Item(cdoSendPassword) = " <enter_password_here>"
  23.          .Update     
  24. End With
  25. Set cdoMessage = CreateObject("CDO.Message")     
  26. With cdoMessage   
  27. Set .Configuration = cdoConfig   
  28.          .From = "from@me.com"
  29.          .To = "to@me.com"
  30.          .Subject = "Sample CDO Message"
  31.          .TextBody = "This is a test for CDO.message"
  32.          .Send   
  33. End With

posted on 2010-06-02 17:30  Ж飞羽忘寒Ж  阅读(545)  评论(0)    收藏  举报