I found several variations to send email via Gmail in VBA and watched a YouTube demo using the sub below.
I tried smtpserverport 25 and 465.
I tried smtpserver smtp.gmail.com and smtp.office365.com.
I have the Microsoft CDO Reference selected.
I verified the Gmail password and get no error messages, no compile errors.
I call the sub from the main routine above via send_email_via_gmail
. I added a Msgbox just past the send statement to make sure the sub is called and it does reach it but I never get the emails. I have O365.
Sub send_email_via_gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxx@gmail.com"
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxx"
myMail.Configuration.fields.Update
With myMail
.Subject = "Test email from Stock App"
.From = "xxxxxxx@gmail.com"
.To = "xxxxxxx@comcast.net"
.TextBody = "Test email from Me to Me"
End With
On Error Resume Next
myMail.Send
Set myMail = Nothing
End Sub
When I remove the On Error
statement I do get a run-time error
The message could not be sent to the SMPT server. The transport error code was 0x80040217. The server response was not available.