ExcelAtEverything
Active Member
- Joined
- Jan 30, 2021
- Messages
- 351
- Office Version
- 2019
- Platform
- Windows
Hello again friends!
The following code works perfectly to send email from my personal gmail, adding an attachment along the way. I need this action to occur 5 times however (without having to change the code and rerun it every time of course). What I mean is that I need the code to loop through & do the same thing 5 times back to back (there will be different recipients & attachments with each send).
Thanks for any help!
The following code works perfectly to send email from my personal gmail, adding an attachment along the way. I need this action to occur 5 times however (without having to change the code and rerun it every time of course). What I mean is that I need the code to loop through & do the same thing 5 times back to back (there will be different recipients & attachments with each send).
Thanks for any help!
VBA Code:
Sub Send_Email_With_Gmail()
Dim newMail As CDO.Message
Dim mailConfiguration As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo errHandle
Set newMail = New CDO.Message
Set mailConfiguration = New CDO.Configuration
mailConfiguration.Load -1
Set fields = mailConfiguration.fields
With newMail
.Subject = "R*** ***** *** **-**-***1"
.From = "r*******g@gmail.com"
.To = "e****3@gmail.com"
.CC = ""
.BCC = ""
' To set email body as HTML, use .HTMLBody
' To send a complete webpage, use .CreateMHTMLBody
.TextBody = "Please see attached report."
.AddAttachment "C:\Users\********\Downloads\****** 7-13-2021 (PDF).pdf"
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.Item(msConfigURL & "/smtpusessl") = True
.Item(msConfigURL & "/smtpauthenticate") = 1
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
.Item(msConfigURL & "/smtpserverport") = 465
.Item(msConfigURL & "/sendusing") = 2
.Item(msConfigURL & "/sendusername") = "r*******g@gmail.com"
.Item(msConfigURL & "/sendpassword") = "T*****4"
.Update
End With
newMail.Configuration = mailConfiguration
newMail.Send
MsgBox "Email has been sent", vbInformation
exit_line:
'// Release object memory
Set newMail = Nothing
Set mailConfiguration = Nothing
Exit Sub
errHandle:
MsgBox "Error: " & Err.Description, vbInformation
GoTo exit_line
End Sub