Salad UN

To Taste The Salad Of Life.

« DedeCMS Digg功能大修改SupeSite无限循环栏目标签 »

CDOSYS用法及函数变量

[ At 2008-12-19 By Ash   0 comments ]

CDOSYS全部用法,大量例子

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

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

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

 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80722 Code detection by Codefense

Copyright 2008 www.s-un.cn. Some Rights Reserved.