Sending e-mail with CDOSYS CDO (Collaboration Data Objects) is a Microsoft technology that is designed to simplify the creation of messaging applications.
CDOSYS is a built-in component in ASP. We will show you how to use this component to send e-mail with ASP.
How about CDONTs? Microsoft has discontinued the use of CDONTs on Windows 2000, Windows XP and Windows 2003. If you have used CDONTs in your ASP applications, you should update the code and use the new CDO technology.
Examples using CDOSYS
发送文本Email:
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.TextBody="This is a message." myMail.Send set myMail=nothing %>
发送文本Email,带有Bcc和CC的:
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.Bcc="someoneelse@somedomain.com" myMail.Cc="someoneelse2@somedomain.com" myMail.TextBody="This is a message." myMail.Send set myMail=nothing %>
发送HTML格式的Email:
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.HTMLBody = "<h1>This is a message.</h1>" myMail.Send set myMail=nothing %>
发送网页
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.CreateMHTMLBody "http://www.w3schools.com/asp/" myMail.Send set myMail=nothing %>
发送本机的网页
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.CreateMHTMLBody "file://c:/mydocuments/test.htm" myMail.Send set myMail=nothing %>
发送带附件的文本email
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.TextBody="This is a message." myMail.AddAttachment "c:\mydocuments\test.txt" '重复多次即可添加多个文件。 myMail.Send set myMail=nothing %>
使用远程Server发送文本Email
<% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="mymail@mydomain.com" myMail.To="someone@somedomain.com" myMail.TextBody="This is a message." myMail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 'Name or IP of remote SMTP server myMail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") _ ="smtp.server.com" 'Server port myMail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _ =25 myMail.Configuration.Fields.Update myMail.Send set myMail=nothing %>
远程Server需要身份验证
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate Const cdoBasic = 1 'basic (clear-text) authentication Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Example CDO Message" objMessage.From = """Me"" <me@my.com>" objMessage.To = "test@paulsadowski.com" objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"
'验证方式, NONE, Basic (Base64 encoded), NTLM objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'SMTP 服务器的用户名 objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"
'SMTP 服务器的密码 objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"
'Server端口(通常为25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'是否使用SSL连接(False 或 True) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'连接smtp的超时时间,单位为秒 objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
objMessage.Send
Send using authentication against a remote server with a file attachment and return receipt and delivery disposition notification requests. In order to use the Delivery Status Notifications (Return Receipt and Delivery Disposition requests) we need to create a reference to the CDO Configuration object in addition to the CDO Message object and set a small number of properties. You must use cdoSendUsingPort (network connection) and not the SMTP server's pickup directory (cdoSendUsingPickup).
Const cdoSendUsingPickup = 1 Const cdoSendUsingPort = 2 '如果使用发送通知,必须使用这个。Const cdoAnonymous = 0 Const cdoBasic = 1 ' clear text Const cdoNTLM = 2 'NTLM '发送状态通知 Const cdoDSNDefault = 0 'None Const cdoDSNNever = 1 'None Const cdoDSNFailure = 2 'Failure Const cdoDSNSuccess = 4 'Success Const cdoDSNDelay = 8 'Delay Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
set objMsg = CreateObject("CDO.Message") set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields With objFlds .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.yourhost.com" .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password" .Update End With
strBody = "This is a sample message." & vbCRLF strBody = strBody & "It was sent using CDO." & vbCRLF
With objMsg Set .Configuration = objConf .To = "test@paulsadowski.com" .From = "me@my.com" .Subject = "This is a CDO test message" .TextBody = strBody 'use .HTMLBody to send HTML email. .Addattachment "c:\temp\Scripty.zip" .Fields("urn:schemas:mailheader:disposition-notification-to") = "me@my.com" .Fields("urn:schemas:mailheader:return-receipt-to") = "me@my.com" .DSNOptions = cdoDSNSuccessFailOrDelay .Fields.update .Send End With
|