I am trying to use VBA in Excel to send a message to an AOL address. I can get Gmail and Comcast to work, but not AOL. Error: -2147220973 - The transport failed to connect to the server. My code originated here: https://www.makeuseof.com/tag/send-emails-excel-vba/
Thank you for your assistance!
Option Explicit
' Source from: https://www.makeuseof.com/tag/send-emails-excel-vba/
Sub Test_Email()
Dim sResult As String
sResult = Send_Emails("somebody@aol.com", "MyPWD", "smtp.aol.com")
MsgBox "Finished: " & vbTab & sResult
End Sub
Public Function Send_Emails(EmailAddress As String, PWD As String, SMTP As String) As String
Dim NewMail As CDO.Message
Dim mailConfig As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo Err:
Debug.Print "Send: " & EmailAddress & vbTab & PWD & vbTab & SMTP
Set NewMail = New CDO.Message
Set mailConfig = New CDO.Configuration
mailConfig.Load -1 'load all default configurations
Set fields = mailConfig.fields
With NewMail
.From = EmailAddress
.To = "myname@comcast.net"
.CC = ""
.BCC = ""
.Subject = "Send Email From an Excel Spreadsheet"
.TextBody = "This is the body of your email. And here is some added data:" & "XXXX"
.AddAttachment ThisWorkbook.path & "\" & "openexcel.txt" ' 'Optional file attachment; remove if not needed.
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.item(msConfigURL & "/smtpusessl") = True 'Enable SSL Authentication
.item(msConfigURL & "/smtpauthenticate") = 1 'SMTP authentication Enabled
.item(msConfigURL & "/smtpserver") = SMTP 'i.e. "smtp.aol.com"
.item(msConfigURL & "/smtpserverport") = 465 'Set the SMTP port Details
.item(msConfigURL & "/sendusing") = 2 'Send using default setting
.item(msConfigURL & "/sendusername") = EmailAddress ' full xxx&aol.com 'Your gmail address
.item(msConfigURL & "/sendpassword") = PWD 'Your password or App Password
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.send
MsgBox "Your email has been sent", vbInformation
Send_Emails = "Success!"
Exit_Err:
'Release object memory
Set NewMail = Nothing
Set mailConfig = Nothing
Exit Function
Err:
Select Case Err.Number
Case -2147220973 'Could be because of Internet Connection
MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " &
Err.Description
Case -2147220975 'Incorrect credentials User ID or password
MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " &
Err.Description
Case Else 'Report other errors
MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " &
Err.Description
End Select
Resume Exit_Err
Resume
End Function
Use the Outlook object model instead of CDO (not supported any longer).
Microsoft Outlook 2010 and later versions include many architectural changes to the client-side MAPI subsystem. Of particular concern are scenarios in which Outlook is configured to use multiple Exchange accounts. Also, CDO 1.2.1 is a 32-bit client library and will not operate with 64-bit versions of Outlook. Given all these factors, CDO 1.2.1 is not supported for use with Outlook 2010 or later versions, and MS does not recommend its use with Outlook 2010 and later versions.
Programs that use CDO should be redesigned to use other Application Programming Interfaces (APIs) instead of CDO. Starting with Outlook 2007, the Outlook object model was greatly expanded to provide functionality that was previously available only by using CDO 1.2.1. The Outlook 2010 and later versions object model includes some new features to expand on this more. For example, the Outlook object model has new functionality to operate correctly with multiple Exchange accounts. The Outlook object model also works for both 32-bit and 64-bit versions of Outlook. Developers should use the Outlook 2010 and later object model instead of CDO 1.2.1. Also, developers can still use Extended MAPI (which requires unmanaged C++) in some scenarios where CDO was required. However, if it is possible, we generally recommend that the Outlook object model is used instead of Extended MAPI.
See Collaboration Data Objects (CDO) 1.2.1 is not supported with Outlook 2010 and later versions for more information.