Email To Gmail Using Access VBA
Send Email from Access to Gmail: We have come up with another more flexible method for sending email using Google Gmail rather than MS Outlook or Exchange Server.Just like our other email routines, this VBA Email transmission method gives you the capability to automatically generating emails from an Access database. It is important to note that you can send your email to anyone on the internet and you aren’t limited to sending emails just to other Gmail users.
Note that we are using Gmail as an Email server and therefore we can send messages from our desktop out through Gmail to anyone on any Email system. One of the benefits is that your email message isn’t retained on your company’s email server. This method is likely to work with other SMTP email systems (Yahoo, Hotmail, etc.) but we haven’t verified those routes.
Our method uses Microsoft’s CDO Message routine which requires no additional references in your Access visual basic module. The CDO message architecture is reference directly on Microsoft’s website.
Let’s get to it – here is the code in its simplest setup:
Public Function send_email()
Set cdomsg = CreateObject(“CDO.message”)
.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 ‘NTLM method
.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “smtp.gmail.com”
.Item(“http://schemas.microsoft.com/cdo/configuration/smptserverport”) = 587
.Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1
.Item(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”) = True
.Item(“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”) = 60
.Item(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “email@example.com”
.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “mypassword”
‘ build email parts
.To = “firstname.lastname@example.org”
.From = “email@example.com”
.Subject = “the email subject”
.TextBody = “the full message body goes here. you may want to create a variable to hold the text”
Set cdomsg = Nothing
In the code above the green text represent comments and the red text variable parts that you need to customize for you data/email accounts.
Also, Gmail has an alternate server port number: 465. Outlook uses port number 25.
Below is an image of the code so you can more easily remove the line breaks.Send Email to Gmail from Access VBA
Running this code from Access visual basic gives you the ability to add recordset operations in order to grab emails from a table and add looping code to loop through address in order to send out multiple emails.
I believe there are CDO message options for such things as blind CCs, regular CCs, message acknowledgement, and attachments. You can find these options here:Microsoft CDO Message Components.
View our 2nd CDO Email example that shows the settings required on the Gmail side:Send Gmail Email From Access
MS Access 2000 Through 2016 and Office 365 & Sharepoint
Microsoft Office VBA, MS Access 2003, 2007, 2010, 2013, 2016