Excel email working in personal Gmail, not in business Gmail Acct

ExcelAtEverything

Active Member
Joined
Jan 30, 2021
Messages
351
Office Version
  1. 2019
Platform
  1. Windows
Hello all,
I pulled the code below from YouTube, and I have it working perfectly with my personal Gmail account so that Excel sends an email with attachment thru Gmail. However when I try to use this exact same code, but I replace my personal Gmail acct with a company-issued Gmail account (it's a Google Workspace business account using the company domain) it returns a SMTP (0x80040217) error when attempting to send from that address. I checked to make sure that the SMTP settings were the same in Gmail settings, and there doesn't appear to be anything different between my personal Gmail acct and the business Gmail account, so I'm not sure how I can repair.

This is the only portion of the code I am changing (in addition to the "From:" email address to the same thing)
VBA Code:
[CODE=vba]        .Item(msConfigURL & "/sendusername") = "r*******g@gmail.com"
        .Item(msConfigURL & "/sendpassword") = "T*****4"
[/CODE]

And here's the full piece. Thanks so much for any help.

VBA Code:
Sub Send_Email_With_Gmail()

    Dim newMail As CDO.Message
    Dim mailConfiguration As CDO.Configuration
    Dim fields As Variant
    Dim msConfigURL As String
   
    On Error GoTo errHandle
   
    Set newMail = New CDO.Message
    Set mailConfiguration = New CDO.Configuration
   
    mailConfiguration.Load -1
   
    Set fields = mailConfiguration.fields
   
    With newMail
        .Subject = "R*** ***** *** **-**-***1"
        .From = "r*******g@gmail.com"
        .To = "e****3@gmail.com"
        .CC = ""
        .BCC = ""
        ' To set email body as HTML, use .HTMLBody
        ' To send a complete webpage, use .CreateMHTMLBody
        .TextBody = "Please see attached report."
        .AddAttachment "C:\Users\********\Downloads\****** 7-13-2021 (PDF).pdf"
    End With
   
    msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
   
    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") = "r*******g@gmail.com"
        .Item(msConfigURL & "/sendpassword") = "T*****4"
       
        .Update
   
    End With
   
    newMail.Configuration = mailConfiguration
    newMail.Send
   
    MsgBox "Email 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
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Enable less secure apps in gmail


Discovered that you can also get this error when Gmail's security settings don't allow messages to be sent from the address you intend to use. I had to enable access for less secure apps for my account in question by:

  1. Logging into the address you want to use for sending email from Excel.
  2. Visit the page Account settings: Your browser is not supported.
  3. Click Enable Less Secure Apps.
  4. Click Done.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top