SMTP Gmail integration fails with 'SendUsing configuration is invalid' error in VBA

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?

It’s an SSL port issue. Gmail supports port 465, but CDO works way better with port 587 using STARTTLS. I had the same problem until I switched to 587 and tweaked the SSL settings. Change your port to 587 and set smtpusessl to True - this combo works every time with Gmail’s App Passwords. Double-check your App Password is copy-pasted exactly as generated (no spaces or dashes). CDO gets weird with Gmail’s SSL on port 465, but 587 with STARTTLS just works. Been using this setup without issues since Google killed Less Secure Apps.

In your approach, you’re creating a new Configuration object but attempting to use it with an email message that has its own configuration. The call to emailMessage.Configuration.Load -1 initializes it with default settings, which then conflicts with your custom configuration. To resolve this, you should apply the SMTP settings directly to the message’s configuration without creating a separate configuration object:

Set emailMessage = New CDO.Message
schemaURL = "https://schemas.microsoft.com/cdo/configuration"

With emailMessage.Configuration.Fields
   .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

This ensures you’re configuring the message’s own configuration object without creating competing instances.

First, make sure 2FA is actually working on your Gmail - app password generation sometimes fails without telling you. Try deleting the .Load -1 line entirely since it can break the config. I ran into this same problem and my antivirus was blocking SMTP on port 465. Turn off your firewall and antivirus temporarily to see if that’s what’s causing it.