asp使用CDO.Message发送邮件详细代码集合



发送文本Email:

1 <%
2 Set myMail=CreateObject("CDO.Message")
3 myMail.Subject="Sending email with CDO"
4 myMail.From="[email protected]"
5 myMail.To="[email protected]"
6 myMail.TextBody="This is a message."
7 myMail.Send
8 set myMail=nothing
9 %>
发送文本Email,带有Bcc和CC的:
01 <%
02 Set myMail=CreateObject("CDO.Message")
03 myMail.Subject="Sending email with CDO"
04 myMail.From="[email protected]"
05 myMail.To="[email protected]"
06 myMail.Bcc="[email protected]"
07 myMail.Cc="[email protected]"
08 myMail.TextBody="This is a message."
09 myMail.Send
10 set myMail=nothing
11 %>

 

发送HTML格式的Email: 
1 <%
2 Set myMail=CreateObject("CDO.Message")
3 myMail.Subject="Sending email with CDO"
4 myMail.From="[email protected]"
5 myMail.To="[email protected]"
6 myMail.HTMLBody = "<h1>This is a message.</h1>" 
7 myMail.Send
8 set myMail=nothing
9 %>

 

发送网页:
1 <%
2 Set myMail=CreateObject("CDO.Message")
3 myMail.Subject="Sending email with CDO"
4 myMail.From="[email protected]"
5 myMail.To="[email protected]"
6 myMail.CreateMHTMLBody "http://www.w3schools.com/asp/" 
7 myMail.Send
8 set myMail=nothing
9 %>
发送本机的网页
1 <%
2 Set myMail=CreateObject("CDO.Message")
3 myMail.Subject="Sending email with CDO"
4 myMail.From="[email protected]"
5 myMail.To="[email protected]"
6 myMail.CreateMHTMLBody "file://c:/mydocuments/test.htm" 
7 myMail.Send
8 set myMail=nothing
9 %>
发送带附件的文本email
01 <%
02 Set myMail=CreateObject("CDO.Message")
03 myMail.Subject="Sending email with CDO"
04 myMail.From="[email protected]"
05 myMail.To="[email protected]"
06 myMail.TextBody="This is a message."
07 myMail.AddAttachment "c:\mydocuments\test.txt" '重复多次即可添加多个文件。
08 myMail.Send
09 set myMail=nothing
10 %>

 

使用远程Server发送文本Email
01 <%
02 Set myMail=CreateObject("CDO.Message")
03 myMail.Subject="Sending email with CDO"
04 myMail.From="[email protected]"
05 myMail.To="[email protected]"
06 myMail.TextBody="This is a message."
07 myMail.Configuration.Fields.Item _
09 'Name or IP of remote SMTP server
10 myMail.Configuration.Fields.Item _
12 ="smtp.server.com"
13 'Server port
14 myMail.Configuration.Fields.Item _
16 =25 
17 myMail.Configuration.Fields.Update
18 myMail.Send
19 set myMail=nothing
20 %>
远程Server需要身份验证
01 Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
02 Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
03   
04 Const cdoAnonymous = 0 'Do not authenticate
05 Const cdoBasic = 1 'basic (clear-text) authentication
06 Const cdoNTLM = 2 'NTLM
07   
08 Set objMessage = CreateObject("CDO.Message"
09 objMessage.Subject = "Example CDO Message" 
10 objMessage.From = """Me"" <[email protected]>" 
11 objMessage.To "[email protected]" 
12 objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
13   
14 objMessage.Configuration.Fields.Item _
16   
17 'Name or IP of Remote SMTP Server
18 objMessage.Configuration.Fields.Item _
20   
21 '验证方式, NONE, Basic (Base64 encoded), NTLM
22 objMessage.Configuration.Fields.Item _
24   
25 'SMTP 服务器的用户名
26 objMessage.Configuration.Fields.Item _
28   
29 'SMTP 服务器的密码
30 objMessage.Configuration.Fields.Item _
32   
33 'Server端口(通常为25)
34 objMessage.Configuration.Fields.Item _
36   
37 '是否使用SSL连接(False 或 True)
38 objMessage.Configuration.Fields.Item _
40   
41 '连接smtp的超时时间,单位为秒
42 objMessage.Configuration.Fields.Item _
44   
45 objMessage.Configuration.Fields.Update
46   
47 objMessage.Send
发送对文件附件和回执,并与远程服务器的身份验证
交付处置通知请求。为了使用的传递状态通知(返回
收据和交付处置的要求),我们需要创建一个引用到CDO的配置
除了CDO Message对象的对象,并设置小数目的属性。你必须
使用cdoSendUsingPort(网络连接),而不是SMTP服务器的拾取目录
(cdoSendUsingPickup)。
01 Const cdoSendUsingPickup = 1 
02 Const cdoSendUsingPort = 2 '如果使用发送通知,必须使用这个。Const cdoAnonymous = 0
03 Const cdoBasic = 1 ' clear text
04 Const cdoNTLM = 2 'NTLM
05 '发送状态通知
06 Const cdoDSNDefault = 0 'None
07 Const cdoDSNNever = 1 'None
08 Const cdoDSNFailure = 2 'Failure
09 Const cdoDSNSuccess = 4 'Success
10 Const cdoDSNDelay = 8 'Delay
11 Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
12   
13 set objMsg = CreateObject("CDO.Message")
14 set objConf = CreateObject("CDO.Configuration")
15   
16 Set objFlds = objConf.Fields
17 With objFlds
18    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
19    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.yourhost.com"
21    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
22    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
23    .Update
24 End With
25   
26 strBody = "This is a sample message." & vbCRLF
27 strBody = strBody & "It was sent using CDO." & vbCRLF
28   
29 With objMsg
30    Set .Configuration = objConf
31    .To "[email protected]" 
32    .From = "[email protected]"
33    .Subject = "This is a CDO test message"
34    .TextBody = strBody
35     'use .HTMLBody to send HTML email.
36    .Addattachment "c:\temp\Scripty.zip"
37    .Fields("urn:schemas:mailheader:disposition-notification-to") = "[email protected]"
38    .Fields("urn:schemas:mailheader:return-receipt-to") = "[email protected]" 
39    .DSNOptions = cdoDSNSuccessFailOrDelay
40    .Fields.update
41    .Send
42 End With

猜你喜欢

转载自blog.csdn.net/kirawoo/article/details/74542743