您的位置:首页 > 编程语言 > ASP

ASP用CDO.Message发送邮件

2007-09-08 17:04 381 查看
ASP用CDO.Message发送邮件

http://www.szasp.cn/HTML/30/31/2007/14670.html

<%
function send_mail(s_email,s_email2,s_topic,s_body)

'参数说明
's_email: 主要邮件地址
's_email2: 备用邮件地址
's_topic: 邮件主题
's_body: 邮件内容

dim eAccount,vTmp,iConf,Flds,oMail

eAccount = "test@smtp.126.com" '这里是你的邮件服务器地址和登陆名,我用的是126的邮箱做的测试

vTmp = Split(eAccount, "@", -1, vbTextCompare)

Set iConf = server.CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
Flds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort这里是发送邮件端口
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserver") = vTmp(1)
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '这里是SMTP服务器端口
Flds("http://schemas.microsoft.com/cdo/configuration/smtpaccountname") = eAccount
Flds("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = eAccount
Flds("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
Flds("http://schemas.microsoft.com/cdo/configuration/sendusername") = vTmp(0)
Flds("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx" '我的126邮箱密码
Flds.Update

set oMail = server.CreateObject("CDO.Message")
oMail.To = s_email
If s_email2<>"" Then
oMail.CC = s_email2
End If
oMail.Subject = s_topic
oMail.HTMLBody = s_body
oMail.From = "test@126.com" '这里必须和上面的登陆名一致


Set oMail.Configuration = iConf
oMail.MimeFormatted = True
oMail.AutoGenerateTextBody = True
oMail.Fields.Update
oMail.HTMLBodyPart.Charset = "gb2312"
oMail.Send

Set oMail = Nothing
Set Flds = Nothing
Set iConf = Nothing

send_mail=true
if err then
err.clear
send_mail=false
end if
end function

If send_mail(test@163.com","test2@163.com","邮件主题","邮件内容")=true Then
'发送成功
Else
'发送失败
End If
%>

另一篇





How to send HTML formatted mail using CDO for Windows 2000 and a remote SMTP service

http://support.microsoft.com/kb/286431
View products that this article applies to.
function loadTOCNode(){}

Article ID:286431
Last Review:August 25, 2005
Revision:3.1
This article was previously published under Q286431

SUMMARY

loadTOCNode(1, 'summary');
This article describes how to send HTML formatted mail using CDO for Windows 2000 (CDOSYS) or CDO for Exchange 2000 (CDOEX) using a remote computer's SMTP service.


Back to the top


MORE INFORMATION

loadTOCNode(1, 'moreinformation');

1.

' Send by connecting to port 25 of the SMTP server.


Dim iMsg 


Dim iConf 


Dim Flds 


Dim strHTML




Const cdoSendUsingPort = 2




set iMsg = CreateObject("CDO.Message")


set iConf = CreateObject("CDO.Configuration")




Set Flds = iConf.Fields




' Set the CDOSYS configuration fields to use port 25 on the SMTP server.




With Flds


    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort


    'ToDo: Enter name or IP address of remote SMTP server.


    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<remote SMTP server>" 


    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10  


    .Update


End With




' Build HTML for message body.


strHTML = "<HTML>"


strHTML = strHTML & "<HEAD>"


strHTML = strHTML & "<BODY>"


strHTML = strHTML & "<b> This is the test HTML message body</b></br>"


strHTML = strHTML & "</BODY>"


strHTML = strHTML & "</HTML>"




' Apply the settings to the message.


With iMsg


    Set .Configuration = iConf


    .To = "<email address>" 'ToDo: Enter a valid email address.


    .From = "<email address>" 'ToDo: Enter a valid email address.


    .Subject = "This is a test CDOSYS message (Sent via Port 25)"


    .HTMLBody = strHTML


    .Send


End With




' Clean up variables.


Set iMsg = Nothing


Set iConf = Nothing


Set Flds = Nothing




MsgBox "Mail Sent!"

2.Edit the sections of the code that are marked "ToDo".
3.Save the file, and then double-click it.

The code creates an HTML-formatted message and sends it using the remote computer's SMTP service.


Back to the top


REFERENCES

loadTOCNode(1, 'references');
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
286430 (http://support.microsoft.com/kb/286430/EN-US/) How To Send HTML Formatted mail Using CDO for Windows 2000 and the Local Pickup Directory
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: