ASP 发送电子邮件

CDONTS / CDO.Message 发信、SMTP 配置、HTML 邮件与附件。

Intermediate
Key Points经典 ASP 靠 CDO.Message 组件发信,走本机或远程 SMTP;CDONTS 已过时,新代码用 CDO。

最简单的发信

ASP
<%
    Dim mail
    Set mail = Server.CreateObject("CDO.Message")

    mail.From = "noreply@example.com"
    mail.To = "user@example.com"
    mail.Subject = "注册确认"
    mail.TextBody = "请点击链接完成注册。"

    ' 指定 SMTP 服务器
    mail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    mail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
    mail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    mail.Configuration.Fields.Update

    mail.Send
    Set mail = Nothing

    Response.Write "邮件已发送"
%>
Output
邮件已发送

sendusing = 2 means network SMTP (1 drops into a local queue directory). All three configuration fields are required; a missing one yields "the transport cannot connect to the server".

需要认证的 SMTP

ASP
<%
    Dim cfg, mail
    Set cfg = Server.CreateObject("CDO.Configuration")
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "apiuser"
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "secret"
    cfg.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    cfg.Fields.Update

    Set mail = Server.CreateObject("CDO.Message")
    Set mail.Configuration = cfg
    mail.From = "noreply@example.com"
    mail.To = "user@example.com"
    mail.Subject = "测试"
    mail.TextBody = "带认证的邮件"
    mail.Send
    Set mail = Nothing
    Set cfg = Nothing
%>

smtpauthenticate = 1 is basic auth. Many providers require an app-specific password rather than the login password, and commonly use port 587 (STARTTLS) or 465 (SSL).

HTML 邮件与附件

ASP
<%
    mail.Subject = "月度报告"
    mail.HTMLBody = "<h1>月度报告</h1><p>详见附件。</p>"
    mail.AddAttachment "C:\data\report.pdf"
    mail.Send
%>

Setting both TextBody and HTMLBody makes CDO build a multipart/alternative message, so text-only clients still see content. With only HTMLBody, text clients show a blank mail.

常见失败原因

SymptomCause
CDO.Message 无法创建IIS SMTP 服务未安装,或 64 位/32 位不匹配
Transport cannot connect端口被封、服务器地址错、未开 TLS
550 relay denied发件人域名与认证账号不一致
中文主题乱码未设置字符集
进入垃圾箱SPF / DKIM 未配
ASP
<%-- 中文主题需要显式编码 --%>
mail.Subject = "=?UTF-8?B?" & Base64Encode("月度报告") & "?="
Hardcoding the SMTP password in .asp hands it to anyone who can read the source, including an attacker exploiting a file-inclusion flaw. At minimum load it into Application from Global.asa, or use a local SMTP relay that needs no auth.

在页面里同步发信的问题

mail.Send is synchronous: a slow SMTP server leaves the user staring at a spinner until timeout. Real projects write mail into a table and let a scheduled job send it; the page only enqueues.

Friends & Partners