In today's fast-paced world, automation has become a crucial tool for streamlining tasks and improving efficiency. One area where automation can make a significant impact is email communication. With Visual Basic for Applications (VBA), users can leverage the power of automation to send emails directly from Excel, streamlining the process and saving valuable time. In this guide, we'll explore how to automate email sending using VBA, focusing specifically on Gmail users who have two-factor authentication (2-FA) enabled and require an app password.
Setting up your Gmail Account
It's essential to ensure that your Gmail account is configured correctly for automation. Here's what you need to do:
Enable Two-Factor Authentication (2-FA)
Go to your Google Account settings and enable 2-FA for added security.
Designing the UserForm
Here is the CODE
Sub SendEmail()
Dim from, recipient, cc, bcc, password, subject, body, attachment As String, enable_html As Boolean
'CONFIGURATION - EDIT THIS
from = "sendermail@gmail.com" 'Insert your own email
recipient = UserForm1.TextBox1.Text ' Recipient 'Insert recipient 'email
password = "myp@ssw0rd" 'Insert your Gmail password or App password '(if you have 2-factor authentication enabled)
subject = UserForm1.TextBox2.Text 'Email subject
body = UserForm1.TextBox3.Text 'The body text of the email
enable_html = False 'Set to True if you want to add HTML to the body text of the email (optional)
'NO NEED TO EDIT ANYTHING BELOW THIS
On Error GoTo Err
Dim mailObj, configObj As Object, fields As Variant, msConfigURL As String
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
'Create objects
Set mailObj = CreateObject("CDO.Message")
Set configObj = CreateObject("CDO.Configuration")
configObj.Load -1
Set fields = configObj.fields
'Set email properties
With mailObj
.subject = subject
.from = from
.to = recipient
End With
If enable_html = True Then
With mailObj
.htmlbody = body
End With
Else
With mailObj
.textbody = body
End With
End If
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") = from
.Item(msConfigURL & "/sendpassword") = password
.Update
End With
mailObj.Configuration = configObj
mailObj.Send
Set mailObj = Nothing
Set configObj = Nothing
Exit Sub
Exit_Err:
Set mailObj = Nothing
Set configObj = Nothing
End
Err:
MsgBox "An error ocurred." & vbNewLine & Err.Number & ": " & Err.Description
Resume Exit_Err
End Sub
Finally, we send the email using the Send method and clean up the resources.
Automate Invoice Processing
Automated invoice processing is transforming financial operations. By embracing this technology, businesses save time and money while gaining a competitive edge.
Unlocking Efficiency with Automated Lead Generation Imagine your team focusing on high-value interactions while AI-powered agents handle lead scoring and qualification. This is the reality of modern lead generation automation.
Unlock Efficiency with Google Sheets Automation As businesses seek to optimize operations, no-code automation tools for Google Sheets and beyond are increasingly valuable. They represent a shift towards more accessible, user-centric technology that empowers individuals to control their digital workflows.
OUR BLOG
Check our other top quality content posts on the blog.
Don’t wait and start
your own project with us today
Happy users
Installed apps
Magni dolores eos qui Ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet





