如何解决使用 VBA 和 CDO 从 Excel 发送电子邮件不可靠?
我使用 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。