ASP用CDO.Message发送邮件

本文介绍如何利用ASP和CDO.Message组件通过远程SMTP服务发送HTML格式的电子邮件,包括配置邮件服务器、设置邮件内容及发送过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值