Send emails from Excel using my Gmail account

Roland Hoelscher

New Member
Joined
Oct 15, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I am developing a VBA solution which will send emails from an Excel spreadsheet using my Gmail account.
I found the code below in a previous MrExcel post (I changed only the TO and FROM email addresses).
When I run this code, after a while, it triggers this Run-time error message and points to the ".Send" line at the end.

1741984367391.png
1741984605625.png


Is the approach in my code the correct way to send emails from Excel using Gmail?
And how can I get this to work?
Thank you very much in advance!!

'============================================================
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
.Update
End With

strbody = "Test Email"

With iMsg
Set .Configuration = iConf
.To = "rolandhoelscher@yahoo.com"
.CC = ""
.BCC = ""
.From = "rolandhoelscher@gmail.com"
.Subject = "New figures"
.TextBody = strbody
.Send
End With

End Sub
 
Could be your firewall blocking it but try changing the 465 in the below to 587
VBA Code:
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
it's been a few years since I've used Gmail rather than Outlook though
 
Upvote 0
My old snippets suggest you could use this configuration:
VBA Code:
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youremail@gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    .Update
End With
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top