Send Gmail Email From Access VBA

Send Gmail Email From Access VBA

The sample database provided gives an example of sending an email through Access using Gmail.  In order for this to work, you must have a Gmail account, and know your user id and password. Also, in your Gmail settings, you will want to turn on less secure apps to avoid transport errors: (

Send Gmail Email From Access VBA

After you set up your Company Emails and Passwords, the database utilizes your Gmail address and password that is selected on the Control Panel tab, to send your email. This is helpful when you have more than one person that needs to send emails. It also uses your customers’ email address that is set up in Contacts. If you have the need to send emails to different people for different reasons (i.e.. Invoices go to and status updates go to, the sample database (below) allows for separate email addresses, and also allows for multiple email addresses. The email function will send to everyone in the field. Separate the addresses by semi-colons or commas.

Here’s the code:

  Dim msg As Object

        Set msg = CreateObject(CDO.Message“)

msg.From = Forms![mainentry]![UserEmail] ‘This is the user email chosen on the Control Panel tab. Must be a GMAIL account. = [Forms]![mainentry]![invoiceemail1]’This is the Contact’s email address(es) where invoices get sent to

        msg.Subject = “Your Invoice is Attached”

        msg.textbody = “Thank you!”

        msg.CC = “”

        msg.BCC = “”

        msg.ReplyTo = Forms![mainentry]![UserEmail] ‘Again, the user email from the Control Panel

        msg.Configuration.Fields(“”) = “”

        msg.Configuration.Fields(“”) = 465

        msg.Configuration.Fields(“”) = 2

        msg.Configuration.Fields.Item(“”) = Forms![mainentry]![UserEmail] ‘Again, the user email from the Control Panel

        msg.Configuration.Fields.Item(“”) = Forms![mainentry]![password]’The user password from the Control Panel

        msg.Configuration.Fields.Item(“”) = True

        msg.Configuration.Fields.Item(“”) = 1


msg.HTMLBody = “<font size=’2′ face=’Verdana, Arial, Helvetica, sans-serif’>”

msg.HTMLBody = “Thank you!”

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””blue””>”

‘Following is the address block that gets inserted at the bottom of the email

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””black””Test Company Name</font><BR>”

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””black””>418 Anywhere Rd.</font><BR>”

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””black””>Ellisville, MS 39437</font><BR>”

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””black””>(555) 555-2675 office</font><BR>”

msg.HTMLBody = msg.HTMLBody & “<font face=””arial“” size=””2″” color=””black””>(555) 555-9532 fax</font><BR>”

msg.HTMLBody = msg.HTMLBody & Forms![mainentry]![UserEmail]’Inserts the users’ email into the address block

msg.HTMLBody = msg.HTMLBody & “<font face=””Arial”” size=””2″” color=””green””><u>” & SW & “</u></font><BR>”

msg.HTMLBody = msg.HTMLBody & “<font face=””Arial”” size=””2″” color=””blue””>”


        MsgBox “Invoice Sent”

‘You can comment out this last line if you don’t want to be notified every time your email is sent.

         Exit Sub


    MsgBox “Error ” & Err.Number & “: ” & Err.Description & ” in ” & _

           VBE.ActiveCodePane.CodeModulevbOKOnly, “Error”

End Sub

For your programming enjoyment we have included a free Access Database Gmail CDO example  download for you to practice with.  Feel free to use the code in your databases.

Visual Basic Tutorials:

Access-Google EarthAge CalculationChange To Proper CaseEmail via Gmail #1
Send Gmail Email #2Inactivity Logout CodeOutlook EmailRead Email Access
Email AttachmentSend Outlook EmailRunning SumDenormalize Records
Stock QuotesCreateQueryDefFind Database PathDetail-Master Update
Data Field ValidationField Value New-OldFindFirstAccess Version
Global Variable ParameterGlobal VariablesActive LabelsFiles List Box
Mail MergeQuick SortSelect CaseAccess Transactions