I’m working on automating email delivery through VBA and need to use Gmail instead of Outlook. I’ve tried multiple approaches found online but they’re all from 2020 or earlier when Gmail still supported Less Secure Apps. Since that feature is discontinued, I set up 2FA and generated App Passwords as suggested in various forums. However, I keep hitting the same wall with a runtime error -2147220960 saying the SendUsing configuration is invalid. I’m using Windows 11 Pro with Microsoft CDO for Windows 2000 library enabled in References. Here’s my current approach:
Option Compare Database
Dim emailMessage As CDO.Message
Dim smtpConfig As CDO.Configuration
Dim configFields As Variant
Dim schemaURL As String
Sub DeliverEmail()
On Error GoTo HandleError
Set emailMessage = New CDO.Message
Set smtpConfig = New CDO.Configuration
emailMessage.Configuration.Load -1
With emailMessage
.Subject = "Automated Test Email"
.From = "[email protected]"
.To = "[email protected]"
.TextBody = "This is an automated message from VBA"
End With
schemaURL = "https://schemas.microsoft.com/cdo/configuration"
Set configFields = smtpConfig.Fields
With configFields
.Item(schemaURL & "/sendusername") = "[email protected]"
.Item(schemaURL & "/sendpassword") = "generated-app-password"
.Item(schemaURL & "/smtpusesssl") = True
.Item(schemaURL & "/smtpauthenticate") = 1
.Item(schemaURL & "/smtpserver") = "smtp.gmail.com"
.Item(schemaURL & "/smtpserverport") = 465
.Item(schemaURL & "/sendusing") = 2
.Update
End With
emailMessage.Configuration = smtpConfig
emailMessage.Send
MsgBox "Message delivered successfully"
Cleanup:
Set emailMessage = Nothing
Set smtpConfig = Nothing
Exit Sub
HandleError:
MsgBox "Delivery failed: " & Err.Description
Resume Cleanup
End Sub
What configuration am I missing to make this work with current Gmail security requirements?