Windows 包括一个称为协作数据对象或 CDO 的对象。此对象允许您使用任何 SMTP 服务器发送电子邮件,前提是满足其他先决条件(防火墙打开、ISP 未阻止端口、在 SMTP 服务器上配置帐户、SMTP 服务器允许中继等)。
我发现的大多数示例都使用后期绑定,这是首选。在我对 XP 的测试中,如果您更喜欢使用早期绑定,正确的库引用似乎是“Microsoft CDO for Windows 2000 Library”。
重要的是要知道,任何时候发送电子邮件都必须通过(或从)某种电子邮件服务器发送。这意味着您必须通过该电子邮件服务器进行身份验证,并且通常还意味着您需要使用该电子邮件服务器上存在的“发件人”电子邮件地址发送电子邮件。
下面是一些使用后期绑定的代码:
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM
Public Sub SendEmail()
Dim imsg As Object
Dim iconf As Object
Dim flds As Object
Dim schema As String
Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields
' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "mail.myserver.com"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "email@email.com"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update
With imsg
.To = "email@email.com"
.From = "email@email.com"
.Subject = "Test Send"
.HTMLBody = "Test"
'.Sender = "Sender"
'.Organization = "My Company"
'.ReplyTo = "address@mycompany.com"
Set .Configuration = iconf
.Send
End With
Set iconf = Nothing
Set imsg = Nothing
Set flds = Nothing
End Sub