推荐答案
// www.javascriptcn.com code example
<%
' 创建 CDO.Message 对象
Set objMessage = Server.CreateObject("CDO.Message")
' 设置邮件的基本信息
objMessage.From = "sender@example.com"
objMessage.To = "recipient@example.com"
objMessage.Subject = "Test Email"
objMessage.TextBody = "This is a test email sent using CDO in ASP."
' 配置 SMTP 服务器
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_username"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
' 更新配置
objMessage.Configuration.Fields.Update
' 发送邮件
objMessage.Send
' 释放对象
Set objMessage = Nothing
Response.Write "Email sent successfully!"
%>本题详细解读
1. CDO.Message 对象
在 ASP 中,CDO.Message 对象用于创建和发送电子邮件。首先,我们需要使用 Server.CreateObject("CDO.Message") 创建一个 CDO.Message 对象实例。
2. 设置邮件基本信息
From: 设置发件人的电子邮件地址。To: 设置收件人的电子邮件地址。Subject: 设置邮件的主题。TextBody: 设置邮件的正文内容。
3. 配置 SMTP 服务器
为了通过 SMTP 服务器发送邮件,我们需要配置以下字段:
sendusing: 设置为2,表示使用网络(SMTP)发送邮件。smtpserver: 设置 SMTP 服务器的地址。smtpserverport: 设置 SMTP 服务器的端口号,通常为25。smtpusessl: 设置为False,表示不使用 SSL 加密。如果需要使用 SSL,可以设置为True。smtpauthenticate: 设置为1,表示使用基本身份验证。sendusername: 设置 SMTP 服务器的用户名。sendpassword: 设置 SMTP 服务器的密码。
4. 更新配置
在设置完所有配置字段后,调用 objMessage.Configuration.Fields.Update 方法来更新配置。
5. 发送邮件
调用 objMessage.Send 方法来发送邮件。
6. 释放对象
发送完邮件后,使用 Set objMessage = Nothing 释放对象,以释放系统资源。
7. 输出结果
最后,使用 Response.Write 输出邮件发送成功的消息。