我使用 VBA 和 CDO 以及通过 AOL 服务器从 Excel 发送电子邮件。
我的问题是它根本不可靠。工作可能有一半的时间,但大部分时间它给了我“无法连接到服务器”的消息。这是我的代码中的问题,还是 AOL 问题?
如果你们都认为这是 AOL 的问题,我可以创建一个 Gmail 帐户并从那里发送,但我的主要地址是 AOL 地址,所以如果可以的话,我想让它工作。
Function SendMail(emSendTo As String, emSubject As String, emBody As String, _
Optional emMerge As String = "", Optional emAttach As String = "", Optional emBCC As Boolean = True)
'This email send method uses microsoft CDO library to send email over the web via SMTP, no need for Outlook
'Comes from my email address so I see replies. Must enable reference to CDO library for spreadsheet
Dim oMessage As Object
Dim oCDOConfig As Object
Dim oSMTPConfig As Variant
'configure for email via SMTP
Set oMessage = CreateObject("CDO.Message")
On Error GoTo Error_Handling
Set oCDOConfig = CreateObject("CDO.Configuration")
oCDOConfig.Load -1
Set oSMTPConfig = oCDOConfig.Fields
With oSMTPConfig
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.aol.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyEmailAddress@aol.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "wynljiefrzinhumg"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Update
End With
With oMessage
Set .Configuration = oCDOConfig
End With
oMessage.Subject = emSubject
oMessage.From = "MyEmailAddress@aol.com"
oMessage.To = emSendTo
'if mail is not being sent to me, then BCC me
If emSendTo <> "MyEmailAddress@aol.com" And emBCC = True Then
oMessage.BCC = "MyEmailAddress@aol.com"
End If
'if a mail merge path is specified, then mail merge and send an HTML email, otherwise use body text
If emMerge = "" Then
oMessage.TextBody = emBody
Else
Call VQSMailMerge(emMerge, oMessage)
End If
'if an attachment file path is specified then attach it to email
If emAttach <> "" Then
oMessage.AddAttachment (emAttach)
End If
oMessage.Send
Error_Handling:
If Err.Description <> "" Then MsgBox "Error emailing, " & Err.Description & " to " & emSendTo
End Function