使用微软CDO.Message发送邮件

2020-07-29 01:21:04
黄豆 UsualTool.com
编程思维/ASP 6194
loading

因客户需求在不安装组件的前提下发送邮件,且使用ASP脚本发信,经过查阅资料,决定使用微软自带的CDO组件。

CDO.Message是微软自带的邮件组件,使用其发送邮件是非常快速的,而不需要安装任何组件实现。

然而网上绝大多数CDO例子均是错误的,经过研究并修正才得以成功。

以下附带源码,供大家参考使用:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
response.charset="utf-8"
Session.CodePage=65001
function SendMail(YourSmtp,YourPort,YourAccount,YourPassword,SendEmail,SendEmails,SendTopic,SendBody,SendAttachment) 
MS_Space = "http://schemas.microsoft.com/cdo/configuration/" 
Set Email = CreateObject("CDO.Message") 
Email.From = YourAccount 
Email.To = SendEmail 
If SendEmails <> "" Then 
Email.CC = SendEmails
End If
Email.Subject = SendTopic
Email.HTMLBody = SendBody
'Email.Textbody = SendBody
If SendAttachment <> "" Then 
Email.AddAttachment SendAttachment
End If
With Email.Configuration.Fields 
.Item(MS_Space&"sendusing") = 2 
.Item(MS_Space&"smtpserver") = YourSmtp
.Item(MS_Space&"smtpserverport") = YourPort 
.Item(MS_Space&"smtpauthenticate") = 1
.Item(MS_Space&"sendusername") = YourAccount 
.Item(MS_Space&"sendpassword") = YourPassword 
.Item(MS_Space&"smtpusessl") = True 
.Item(MS_Space&"smtpconnectiontimeout") = 20
.Update 
End With 
Email.BodyPart.Charset = "UTF-8"
Email.Send 
Set Email=Nothing 
SendMail=True 
If Err Then 
Err.Clear 
SendMail=False 
End If 
End Function
'调用方法
If SendMail("smtp.xx.com","xx@xx.com","465","xxxxxx","xxx@xxx.com","","测试邮件","邮件内容","")=True Then 
response.write"发送成功" 
Else 
response.write"发送失败" 
End If
%>

还等什么,试试吧!

+分享给朋友+
郑重提醒:部分素材来源于互联网,如果侵犯了您的权利,请及时联络我们更正,谢谢合作,电邮:usualtool@qq.com