I am using the code below to try and send an email in Excel using VBA. I keep getting an error:
Run-time error '-2147220973 (80040213)': The transport failed to connect to the server.
I have tried changing ports (25, 465, 587), changing email servers (smtp.gmail.com, smtp.live.com), tried both gmail and Hotmail, enabled less secure on both gmail accounts, even setup Outlook and disabled the firewall in Avast.
Here's the code:
Sub CDO_Mail_Small_Text()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 '587 '465 '25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xyz@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123456"
.Update
End With
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
With iMsg
Set .Configuration = iConf
.To = "charlie@gmail.com"
.CC = ""
.BCC = ""
.From = "xyz@gmail.com"
.Subject = "New figures"
.TextBody = strbody
.Send
End With
End Sub
I now am beginning to think it has to do with the fact I'm using Windows 10 64 bit and Office 2016 64 bit. - Just a hunch, also, I'm new to 64 bit, love it, but am learning.
Can some one advise if I'm on the right track and how I can go about getting this to fly. If I'm not on the right track, what else might be the hangup?
I searched the Search Q&A but haven't found anything specific.