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: (https://www.google.com/settings/security/lesssecureapps)
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 janedoe@company.com and status updates go to suesmith@company.com), 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.
msg.to = [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(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “smtp.gmail.com”
msg.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 465
msg.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
msg.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = Forms![mainentry]![UserEmail] ‘Again, the user email from the Control Panel
msg.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = Forms![mainentry]![password]’The user password from the Control Panel
msg.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”) = True
msg.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1
msg.Configuration.Fields.Update
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””>”
msg.Send
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
errHandler:
MsgBox “Error ” & Err.Number & “: ” & Err.Description & ” in ” & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, “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: