I have VBA code to send single emails via Excel (through Gmail).
Sub Gmail_Bulk_Sending()
Dim NewMail As CDO.Message
Dim mailConfig As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo Err:
'early binding
Set NewMail = New CDO.Message
Set mailConfig = New CDO.Configuration
'load all default configurations
mailConfig.Load -1
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.From = "myemail"
.To = ""
.CC = ""
.BCC = ""
.Subject = "Hello There"
.TextBody = "I really want to this VBA code to work"
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.Item(msConfigURL & "/smtpusessl") = True 'Enable SSL Authentication
.Item(msConfigURL & "/smtpauthenticate") = 1 'SMTP authentication Enabled
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
.Item(msConfigURL & "/smtpserverport") = 465 'Set the SMTP port Details
.Item(msConfigURL & "/sendusing") = 2 'Send using default setting
.Item(msConfigURL & "/sendusername") = "myemail" 'Your gmail address
.Item(msConfigURL & "/sendpassword") = "XXXXXXXXX" 'Your password or App Password
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.Send
MsgBox "Your email has been sent", vbInformation
Exit_Err:
'Release object memory
Set NewMail = Nothing
Set mailConfig = Nothing
End
Err:
Select Case Err.Number
Case -2147220973 'Could be because of Internet Connection
MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description
Case -2147220975 'Incorrect credentials User ID or password
MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description
Case Else 'Report other errors
MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.Description
End Select
Resume Exit_Err
End Sub
My Excel file is set up like this:
Column A = email addresses
Column B = Name of Persons corresponding to email addresses
My goal is to send multiple emails to different people (ex: A1 = email person 1) with .TextBody referencing the cell has the name (ex: B1 = name of person 1)
Example of the two variables and how it would look like in an email:
.To: Reference to cell "A1"
.TextBody: Hey "NAME" (from cell B1), I would like to...
I have seen some help on this but it is always Outlook and that code does not work with Gmail.