Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to Send Email from Excel using Gmail
Option Explicit
' 4 variables below require configuration.
' Once you get this working, refactor, etc.
Sub CDO_Mail_Small_Text_2()
Dim user As String
Dim pass As String
Dim port As Integer
Dim receiverEmail As String
Dim fromEmail As String
'change these variables:
user = ""
pass = "YourGmailPassword"
fromEmail = """John Doe"" <>"
receiverEmail = ""
'If you get this error: "The transport failed to connect to the server..."
' then change the SMTP port from 465 to 25
port = 465
'If you get another error, you may need to enable the "Less Secure" option for GMail:
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("") = True
.Item("") = 1
.Item("") = user
.Item("") = pass
.Item("") = ""
.Item("") = 2
.Item("") = port
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 = receiverEmail
.CC = ""
.BCC = ""
.From = fromEmail
' Note: The reply address is not working if you use this Gmail example
' It automaticaly uses your Gmail address. But you can add this line to change the reply address:
'.ReplyTo = ""
.Subject = "Important message"
.TextBody = strbody
End With
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
End Sub
Copy link

bergerjac commented Jul 28, 2018

Here's a great starter's resource: Sending mail from Excel with CDO by Ron de Bruin

Copy link

bergerjac commented Jul 28, 2018

This is the accompanying Google Doc on How to Send Email from Excel using Gmail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment