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

ExcelAtEverything

Active Member
Joined
Jan 30, 2021
Messages
322
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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,144,630
Messages
5,725,401
Members
422,623
Latest member
Dave52

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
Top