In this tutorial, I will walk you through how to write a VBA script to send an email from your Gmail account.
Important: Makes sure you activate Microsoft CDO for Windows 2000 library first.
Email Provider | IMAP Settings | POP Settings | SMTP Settings |
Gmail |
Server: imap.gmail.com Port: 993 Encryption: SSL/TLS |
Server: pop.gmail.com Port: 995 Encryption: SSL/TLS |
Server: smtp.gmail.com Port: 465 Encryption: SSL/TLS
|
Outlook.com |
Server: Port: 993 Encryption: SSL/TLS
|
Server: Port: 995 Encryption: SSL/TLS |
Server: Port: 587 Encryption: STARTTLS |
Office 365 |
Server: outlook.office365.com Port: 993 Encryption: SSL/TLS
|
Server: outlook.office365.com Port: 995 Encryption: SSL/TLS |
Server: smtp.office365.com Port: 587 Encryption: SSL/TLS |
iCloud |
Server: imap.mail.me.com Port: 993 Encryption: SSL/TLS |
Not available
|
Server: smtp.mail.me.com Port: 587 Encryption: STARTTLS
|
Hi there,
I use your script for my powerpoint, however i get a error on these:
Dim newMail As CDO.Message
Dim mailConfiguration As CDO.Configuration
Datatype not defined?
Can you help me out?
There should be a video included in the tutorial for some reason it is not showing. Anyway, there is a library reference you need to turn on. I have included the video + the timestamp.
https://youtu.be/yFm1bPYk28k?t=50
Hello,
Im using an Excel spreadsheet to send emails via gmail. I would like to be able to track responses to my email campaign. How would you go about setting that up?
Multiple choice response, Credit / Refund / Donate (if donate, identify an amt)
How do you setup the link between the excel spreadsheet and the return email to record the response for each recipient?
I thought perhaps generate a unique ID in the xls, include it in the email and search for it in the reply to tie the responses. Is there an easier way?
Also, there could be multiple records for the same person/family. would need to go through the xls and retrieve all related records, then send email with information from all the records found.
thanks for your help
This is pretty complicated thing you want there. Hum… to be honest, I don’t think Excel is really capable to do that.
Hi,
I have used your codes, but I got an error like this:
Sign-in attempt was blocked for your linked Google Account
Please help me to fix this error.
You may need to turn on allow less secure app setting first.
https://myaccount.google.com/lesssecureapps?pli=1
Thank you, but I have another problem with this now which says “the server respond was not available”. Can you help me with this?
How do I copy something to the Body of the email from excel file?
Anyone know a way to do this in reverse?
Send an email through Outlook based on Data in Google Sheets?
Hi,
I have tried this code in Microsoft Access and when I get to line 33 of the above code:
.Item(msConfigURL & “/smtpusessl”) = True
I get runtime error 348 “Object doesn’t support this property method”
What am i doing wrong?
Or is the Excel VBA different than the Access VBA?
I got it to work on Access by changing the code a bit:
Sub Send_Email_With_Gmail()
Dim newMail As Object
Dim mailConfiguration As Object
Dim fields As Variant
On Error GoTo errHandle
Set newMail = CreateObject(“CDO.Message”)
Set mailConfiguration = CreateObject(“CDO.Configuration”)
mailConfiguration.Load -1
Set fields = mailConfiguration.fields
With fields
.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”) = “”
.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “”
.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
With newMail
.Subject = “Hello World”
.From = “”
.To = “”
.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
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
Hi!
Thank you for this, it seems very useful and it is very clear.
I am getting the following error: Error occured while sending the email !! — ActiveX component can’t create object
Do you know what is happening and how can i fix it?
Thanks!
A
Have you activated Microsoft CDO for Windows 2000 library? (Tools > Library References)
hey I’m still getting a error despite turning on imap, pop and access to less secure apps
“the message could not be sent to the SMTP server. The transport error code was 0x80040217. the server response was not available”
Could this be because im using a corporate gmail account and its still being blocked?
Thanks in advance, this is great
Thank you for sharing. How does the code look like if we want to send the email to many Gmail recipients and copy the image of a range of cells to the body of email?
Hi Amir,
Did you find any solution for your problem? I have the same problem.
Paul
Hi,
Did you find any solution? I
Hi,
I get the following error:
Error: At least one of the Form or Sender fields is required, and neither was found.
Please help
Thanks
hey I’m also still getting a error despite turning on imap, pop and access to less secure apps
“the message could not be sent to the SMTP server. The transport error code was 0x80040217. the server response was not available”
Could this be because im using a corporate gmail account and its still being blocked?
Thanks in advance, this is great
Hi,
I used your code and getting error : The “SendUsing” Configuration value is invalid. I made it equal to 2 like you did?
Could you please assist?
https://support.google.com/accounts/answer/6010255?hl=en#more-secure-apps-how&zippy=%2Cuse-more-secure-apps
Less secure apps & your Google Account
To help keep your account secure, starting May 30, 2022, Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
Please note this deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.
For more information, please continue reading.
Special Note on Apple Device Sign-Ins. Users who have not recently signed into their Google Account using only username and password will be able to only make new sign in attempts using the Google account type starting from February 28, 2022. Existing users may continue to sign into their Google Account using their username and password until May 30, 2022.
Solution?
Thanks for the update, much appreciated.
hallo,
very nice code thx for the tut its working fein with gmail for me but its not work wich office365 smtp .. how can i done this ?? i have office365 email adress of my own domain like myname@mydomain.com