Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Last active May 6, 2019 02:45
Show Gist options
  • Save DataSolveProblems/3f4656da63c2eee8698e87a53c0192ed to your computer and use it in GitHub Desktop.
Save DataSolveProblems/3f4656da63c2eee8698e87a53c0192ed to your computer and use it in GitHub Desktop.
'CDO Documentation https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/ms877946(v%3dexchg.65)
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 = "Hello World"
.From = "<optional>"
.To = "<recipient email address>"
.CC = ""
.BCC = ""
' To set email body as HTML, use .HTMLBody
' To send a complete webpage, use .CreateMHTMLBody
.TextBody = "This is a test email."
.AddAttachment "C:\Users\jiejenn\Desktop\Tutorial\Send Gmail Using VBA\MyLogo.png"
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") = "<Your email address>"
.Item(msConfigURL & "/sendpassword") = "<Your Password>"
.Update
End With
newMail.Configuration = mailConfiguration
newMail.Send
MsgBox "E-Mail 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment