标签归档:CDO.Message

Sending E-Mail Without Installing the SMTP Service

Microsoft® Windows® 2000 Scripting Guide

The script shown in Listing 17.23 can be used as long as the SMTP service is installed on the computer where the script is running. Obviously, this poses a problem: Most likely, you do not want the SMTP service to be installed and running on every one of your computers. 继续阅读

VBS+CDO.Message发送邮件

发送文本邮件

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>


发送带有 Bcc 和CC 域的邮件:

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.Bcc="someoneelse@somedomain.com"
myMail.Cc="someoneelse2@somedomain.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>


发送 HTML 格式邮件:

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.HTMLBody = "<h1>This is a message.</h1>" 
myMail.Send
set myMail=nothing
%> 

发送包含WEB连接页的HTML格式邮件:

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "http://www.2mysite.net/asp/" 
myMail.Send
set myMail=nothing
%> 


发送HTML电子邮件(发送一个本地计算机上的网页):

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "file://c:/mydocuments/test.htm" 
myMail.Send
set myMail=nothing
%>

发送包含附件的邮件:

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.AddAttachment "c:\mydocuments\test.txt"
myMail.Send
set myMail=nothing
%> 

使用远程邮件服务器的服务发送邮件:

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@2mysite.net"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
="smtp.server.com"
'Server port
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
=25 
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
%> 

SMTP要求身份验证的代码

<%

Call send(strBody)
'*************************************************************************
' 邮件发送模块(公用模块一)
' 函数:
' 1. send(strBody)    设置调用相关参数
' 2. sendMail(SMTPSVR,User,Password,MailFrom,MailTo,Subject,Body)    
'    具体的邮件发送功能模块
'*************************************************************************
Sub send(strBody)
    On Error Resume Next
    SMTPSVR = "smtp.2mysite.net"    'smtp server
    User = "username@2mysite.net"    'username
    Password = "password"    'password
    MailFrom = "username@2mysite.net"    'sender
    MailTo = "517sou@msn.com"    'receiver
    Subject = "我们的测试邮件"    'subject
    Body = "测试windows 2003中的邮件发送!"
    If Err.number <> 0 Then
        response.Write err.Description
    Else
        Call sendMail(SMTPSVR,User,Password,MailFrom,MailTo,Subject,Body)
        response.Write "OK"
    End If
End Sub

Sub sendMail(SMTPSVR,User,Password,MailFrom,MailTo,Subject,Body)
    Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
    Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
    Const cdoAnonymous = 0 'Do not authenticate
    Const cdoBasic = 1 'basic (clear-text) authentication
    Const cdoNTLM = 2 'NTLM

    Set objMessage = CreateObject("CDO.Message") 
    objMessage.Subject = Subject 
    objMessage.Sender = MailFrom 
    objMessage.To = MailTo
    objMessage.TextBody = Body
    'objMessage.AddAttachment strAddfile
    '==This section provides the configuration information for the remote SMTP server.
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPSVR
        'Type of authentication, NONE, Basic (Base64 encoded), NTLM
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
        'Your UserID on the SMTP server
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusername") = User
        'Your password on the SMTP server
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Password
        'Server port (typically 25)
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        'Use SSL for the connection (False or True)
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
        'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
    objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    objMessage.Configuration.Fields.Update
    '==End remote SMTP server configuration section==
    objMessage.Send
End Sub
%>