目录
1. CDOSYS 简介
CDOSYS(Collaboration Data Objects for Windows 2000)是 ASP 内置的邮件发送组件,取代了旧版的 CDONTS,支持 SMTP 服务器发送电子邮件。CDOSYS 适用于 IIS 5.0 及以上版本,并可用于 Windows Server 2003 及后续版本。
📌 CDOSYS 主要特点:
- 支持 SMTP 服务器发送邮件
- 支持 HTML 格式邮件
- 可添加附件
- 可自定义邮件头
2. CDOSYS 发送邮件的基本步骤
CDOSYS 发送电子邮件需要以下步骤:
- 创建
CDO.Message
对象 - 设置邮件内容(收件人、发件人、主题、正文)
- 配置 SMTP 服务器信息
- 发送邮件
3. 完整示例代码
以下是使用 CDOSYS 发送电子邮件的基本示例:
<%
Dim objMessage
Set objMessage = Server.CreateObject("CDO.Message")
' 设置发件人和收件人
objMessage.From = "your_email@example.com"
objMessage.To = "recipient@example.com"
objMessage.Subject = "CDOSYS 发送邮件测试"
' 设置邮件正文(HTML 格式)
objMessage.HTMLBody = "<h2>你好,这是 CDOSYS 发送的 HTML 邮件</h2>"
' 配置 SMTP 服务器
Dim objConfig, Fields
Set objConfig = Server.CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_email@example.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
' 绑定配置到邮件对象
Set objMessage.Configuration = objConfig
' 发送邮件
objMessage.Send
' 清理对象
Set objMessage = Nothing
Set objConfig = Nothing
Response.Write "邮件发送成功!"
%>
📌 代码说明:
objMessage.From
和objMessage.To
:定义发件人和收件人地址objMessage.Subject
:设置邮件主题objMessage.HTMLBody
:设置 HTML 格式的邮件正文- SMTP 服务器配置:
smtpserver
:SMTP 服务器地址(如smtp.example.com
)smtpserverport
:SMTP 服务器端口(如25
、587
或465
)smtpauthenticate
:SMTP 身份验证(1 = 需要身份验证)sendusername
和sendpassword
:SMTP 账户用户名和密码smtpusessl
:是否启用 SSL 加密(True
或False
)
4. CDOSYS 配置 SMTP 服务器
常见 SMTP 服务器地址
邮箱服务商 | SMTP 服务器地址 | 端口(TLS/SSL) |
---|---|---|
Gmail | smtp.gmail.com | 587 (TLS) / 465 (SSL) |
Outlook | smtp.office365.com | 587 (TLS) |
Yahoo | smtp.mail.yahoo.com | 465 (SSL) |
163 邮箱 | smtp.163.com | 25 / 465 |
QQ 邮箱 | smtp.qq.com | 25 / 465 |
示例:使用 Gmail 发送邮件
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_email@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
5. 常见错误及解决方案
1. 错误:The transport failed to connect to the server.
📌 原因:SMTP 服务器地址或端口错误
✅ 解决方案:
- 确保 SMTP 服务器地址正确
- 确保 SMTP 服务器端口正确(25、587、465)
2. 错误:The server rejected one or more recipient addresses.
📌 原因:邮件地址错误或服务器拒绝
✅ 解决方案:
- 确保收件人邮件地址正确
- 使用授权的 SMTP 账号发送邮件
3. 错误:The message could not be sent to the SMTP server. The transport error code was 0x80040217.
📌 原因:SMTP 认证失败(用户名或密码错误)
✅ 解决方案:
- 确保 SMTP 账户和密码正确
- 检查是否开启了SMTP 允许外部发送(如 Gmail 需要开启低安全性应用访问)
4. 错误:CDO.Message.1: SendUsing configuration value is invalid.
📌 原因:sendusing
配置值错误
✅ 解决方案:
sendusing
选项应设置为2
,表示使用外部 SMTP 服务器
6. 参考资料
- Microsoft Docs – CDOSYS 邮件发送
- W3Schools – ASP 发送邮件
- Google Support – Gmail SMTP 服务器设置
7. 出站链接
这样,你可以轻松使用 CDOSYS 在 ASP 中发送电子邮件,并解决常见错误!🚀
发表回复