Using Excel VBA to send emails from my Gmail account

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

I am trying to use my excel workbook with VBA to send emails to my employees through my Gmail account. I found many different types of code through my internet search but not sure what to use.

I have already created a workbook that will populate the current employee’s emails on worksheet 1 in cell A1.

The email message (may change) that I would like to email to the employee’s is located on worksheet 1 in cell A3.

Also on some occasions I may also include an attachment (most likely a PDF)

I also have set a reference to Microsoft CDO 1.21 Library, via Tools > References in the VBA editor.

Here is some code I previously using for Outlook if that helps

Code: Select all

Private Sub CommandButton1_Click()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim i As Long
    Dim fd As FileDialog
    Dim strbody As String
    
    Shell "Outlook.exe", vbNormalFocus
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
            
    strbody = ("Hi ") & Sheets("Sheet1").Range("A1") & vbNewLine 'emails addresses

    On Error Resume Next
    With OutMail
        .To = Sheets("Sheet1").Range("A1")    'email addresses
        .BCC = ""
        .Subject = Sheets("sheet1").Range("D3") 'email message
        .Body = strbody
        'Attach documents here
        Set fd = Application.FileDialog(msoFileDialogOpen)
        fd.AllowMultiSelect = True
        If fd.Show Then
            For i = 1 To fd.SelectedItems.Count
                .Attachments.Add fd.SelectedItems(i)
            Next i
        End If

        Set fd = Nothing
        .Display    'or use .Display or .Send

    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Excel VBA to send emails from my Gmail account

Post by HansV »

Try this. Don't forget to change the name and password of the gmail account and the "From" address.

Code: Select all

Private Sub CommandButton1_Click()
    Dim i As Long
    Dim fd As FileDialog
    Dim strTo As String
    Dim strSubject As String
    Dim strBody As String

    On Error GoTo eh:

    'create a CDO object
    Dim gMail As CDO.Message
    Set gMail = New CDO.Message

    'Enable SSL Authentication
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

    'Make SMTP authentication Enabled=true (1)
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

    'Set the SMTP server and port Details
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Set your username and password for your own gmail account here
    gMail.Configuration.Fields.Item _
       ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "johnsmith@gmail.com"
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*********"

    'Update the configuration fields
    gMail.Configuration.Fields.Update

    'set the email properties and the file to send
    With gMail
        .Subject = Sheets("Sheet1").Range("D3").Value
        .From = "stitchmel@gmail.com"
        .To = Sheets("Sheet1").Range("A1").Value 'email address
        .TextBody = "Hi " & Sheets("Sheet1").Range("A1").Value & _
            vbNewLine & Sheets("Sheet1").Range("A3").Value
   End With

   'select the file(s) to send with the microsoft file dialog box
    Dim dlgFile As FileDialog
    Dim strItem As Variant
    Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
    dlgFile.AllowMultiSelect = True
    If dlgFile.Show Then
        For Each strItem In dlgFile.SelectedItems
            gMail.AddAttachment strItem
        Next strItem
    End If

    'send the mail
    gMail.Send
    Exit Sub
eh:
    MsgBox "Email creation failed!"
End Sub
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

I made the changes you suggested but I'm not sure what the "Form" address is?
When I run the code, I receive the message "Email creation failed!"

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Excel VBA to send emails from my Gmail account

Post by HansV »

I'm afraid I don't know enough about CDO to troubleshoot. Anyone else?
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

I wanted to see if I could find the problem, so after I disabled the FileDialog and eh, I then received and error message on the “gMail.Send” line
error message.jpg
The username and password is correct, could it be something I need to do on my Gmail account?
You do not have the required permissions to view the files attached to this post.

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

ABabeNChrist wrote:
13 Nov 2022, 17:33
...could it be something I need to do on my Gmail account?
Possibly.
You must enable less secure apps in gmail account
https://www.youtube.com/watch?v=Ee7PDsbfOUI
https://hotter.io/docs/email-accounts/secure-app-gmail/

_._____________________________________________________________________________
ABabeNChrist wrote:
13 Nov 2022, 16:25
not sure what the "Form" address is?
Do you mean From
That is same as your username.......

Code: Select all

'Set your username and password for your own gmail account here
    gMail.Configuration.Fields.Item _
       ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourGMailName@gmail.com"

Code: Select all

      .from = "YourGMailName@gmail.com"

Here is another syntax that I use

Code: Select all

      .from = """ABabeNChrist"" <YourGMailName@gmail.com>"
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

Thank you DocAElstien I watched the video you provided but my Google account did not show the same process, not sure where it is at..... Upon further digging around I believe since I have a 2 step verification to sign-in this could be causing me my problem. I will try disabling first and see if it helps.

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

Yes, actually I was about to post again.
On testing some of my older CDO send mail codings, I find now that they are no longer working. Indeed it would appear that GMail have changed things, :sad:

We now need to arrange a different password, an additional one to our normal password. I will need to look into this further.
Here is one possible start point:
https://stackoverflow.com/questions/728 ... ional-info

If I make any progress I will report back

_._____

By the way, In the past I have often experienced various problems using gmail accounts in these CDO sending type codings. However, when I use identical codings, but use a
German Freemail, xxxx@t-online.de account
instead of the
G Mail, xxxx@gmail.com account
then I find that the codings are much more stable and reliable ( To use a German Freemail, xxxx@t-online.de account in such CDO send Mail codings , one has always needed to create a seperate additiopnal password to use in the coding. So it would appear that G Mail are now requiring something similar )



Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

DocAElstein wrote:
13 Nov 2022, 20:43
one has always needed to create a seperate additiopnal password to use in the coding. So it would appear that G Mail are now requiring something similar )
DocAElstein I tried both a free version of Gmail and a Gmail paid version domain name, no success. I also turned off the Gmail signing in with 2-step verification, still nothing. :hairout:

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

Hello,

I shall keep looking into this, because I want my CDO send codings to work again with gmail. - I will report back here if I make any progress. I expect I probably will sort it out eventually,
but it may take a few days as I am busy on other things just now.

Just some things to bear in mind in the meantime
_ I have always experienced problems with gmail accounts in these sorts of codings.
Some of these are caused by reasons we cannot know fully:, beacuse...
Gmail, and Google generally, like to conrol different people in different ways. Issues of your country of origin can also come in. Some of my accounts and some of my codings have been registered by, and used by, people in different countries. Despite having identical settings, some accounts work differently in the codings and occasionally require different settings. Some work better and more consistently than others.
My best solution to date involves a coding that loops through a few gmail accounts, also sometimes trying them with slightly different parameters in the codings. Finally, as a last resort, for the occasions that no gmail account works, it tries one or more of my German Telekom accounts. Those German Telekom accounts almost always work by anyone, anywhere.

_ Many people use Outlook based coding rather than CDO based coding. ( I have no experience with Outlook based coding so cannot help, but someone else may be able to give you an alternative outlook based codoing )

_ if you have a spare free gmail account, registered in a similar way to the main gmail that you finally want to use, and care to share the username and password with me via private message , then if I get my coding working, I can also test that account: Once again this is because as I mentioned, who registered an account and where it was registered, effects what and how gmail accounts can be used in CDO based coding:

_ ( I assume you are in Texas, USA? - and have registered the gmail accounts and tried to use the codings there? If this is not the case, then can you tell me what is your country. If you don‘t want to share this information publicly in this post, you can tell me via Private message. I ask because, as I already mentioned, this can effect how the coding works and what parameter settings are required, )


Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

OK, so I thought I would try my luck and just quickly make one attempt to get an old CDO gmail send code of mine to work again.

To my amazement it worked first time. :-)

I have to go out now. Tomorrow I will try again using some other gmail accounts and carefully document with pics what I do. Then I will pass on that info here.

Here is the shortened story to be going on with:
_1) You must enable (Turn on) 2 step verification on the gmail account***
_2) You must generate an "Apps passwword"
_3) That "Apps passwword" is now an extra second password for that gmail account which, amongst other things, is what you use in the sendpassword part of the configurations in a CDO send message type coding. ( In other words, you no longer use your main password in the coding. Instead use the generated "Apps password". )

Note also, that although you still use your main password for most things, as before, but you may now occasionally be asked, for security reasons, to confirm your "Apps password"
***I would strongly recommend trying this out first on a spare gmail account if you have one, as I do not know yet what other consequences and surprises may be awaiting once you make these changes to your gmail account


So at the end of the day, Google have made it even more complicated to do what you want to do, but I believe in this case, at first glance, the motives may be reasonable: It is attempting to make your use of your gmail account more secure…..

Alan

_.________________________________________________________________________________________________________

Edit: here is the full story : https://excelfox.com/forum/showthread.p ... #post19038
https://excelfox.com/forum/showthread.php/2380-Tests-and-Notes-for-EMail-Threads?p=19038#post19038
Last edited by DocAElstein on 17 Jan 2023, 18:53, edited 3 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

I reestablished Gmail's recommended 2 step verification with a verification sent to my phone. Gmail also sent me a list of verification code numbers that I believe can only be used once, once all the numbers are used up, I can then request more. Instead of using number verification I am going to try to use 2 step verification that alerts my phone with a message for approval or disapproval, fingers crossed

here is a screen shot of current settings
backup codes.jpg
You do not have the required permissions to view the files attached to this post.

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: Using Excel VBA to send emails from my Gmail account

Post by SpeakEasy »

>Here is the shortened story to be going on with
That's the approach I successfully used for some years for a mailing list. Don't recall bumping into any unexpected complications.

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

SpeakEasy wrote:
14 Nov 2022, 14:24
>Here is the shortened story to be going on with
That's the approach I successfully used for some years for a mailing list. Don't recall bumping into any unexpected complications.
Thx, good to know.
I never used that approach before yesterday in gmail. Instead I used the way a lot of people use, which is just to use the normal password that you use to log in normally in your gmail account. You could do that provided that in your settings you enabled the option to allow unsecured apps. But it appears that for most people on the simple free Google gmail, they have stopped allowing you to do this since the end of May this year.
I expect this other approach has been available in parallel for a while, but I never knew about it. As far as I know, this extra password for Email programs is the way German Telekom, which I use mostly, has always done. I don’t think they ever allowed you to use your normal log in password. So far, my German Telekom accounts work best most consistently in these CDO send mail codings, all other providers I have tried, including gmail , always give inconsistent problems from time to time.

_.______________________________________________________________________________-
DocAElstein wrote:
14 Nov 2022, 10:09
.. Tomorrow I will try again using some other gmail accounts and carefully document with pics what I do. Then I will pass on that info here.
I have tried a few of my old CDO send codings now, with a few different gmail accounts, and now have them all working again, using the (2 – Step Verification) Apps Password approach

Here you are, the full story….
https://excelfox.com/forum/showthread.p ... #post19036
https://excelfox.com/forum/showthread.p ... #post19037
https://excelfox.com/forum/showthread.p ... #post19038
https://excelfox.com/forum/showthread.p ... #post19039
https://excelfox.com/forum/showthread.p ... #post19040



The three basic parts are
Part 1
Log in and enable 2 – Step Verification https://excelfox.com/forum/showthread.p ... #post19038
https://excelfox.com/forum/showthread.p ... #post19038

Part 2 Generate Apps Password https://excelfox.com/forum/showthread.p ... #post19039
https://excelfox.com/forum/showthread.p ... #post19039

Part 3 Using a gmail "Apps password" in a CDO send mail coding https://excelfox.com/forum/showthread.p ... #post19040
https://excelfox.com/forum/showthread.p ... #post19040

_.____

Part 1 and part 2 are fairly simple, straight forward, and intuitive, provided you keep your wits about you and can and do work quickly.
Once you done it a few times, you can do it in a matter of seconds
But its easy to run into problems the first time and / if you are unfamiliar with gmail and what is going on

A couple of things to be careful about:
_(i) Google gmail is very security sensitive. When you start doing these things you are likely to get security warnings sent to you. Mostly they will ask you to confirm that you are the person doing it. You should try to respond to that quickly, or else they may block you now or in the future. If they start putting a block on you, you can get in a real spaghetti mess trying to sort things out, - you end up in a vicious circle being bombarded by security warnings requiring you to respond quickly, and then a simple task can take hours if not days.

_ (ii) Despite (i) , I have noticed that Google themselves seem to have a security problem which can really get you in a spaghetti mess if you have and use more than one gmail account. They seem to have some cross wires in their attempts to integrate things, spy on you and control you: It can happen that as you navigate through your settings you suddenly find yourself unexpectedly in a different account!!!!
So keep your eye on the thing top right that tells you what account you are in.



Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

Hi DocAElstein I tried your suggestions and I still get the same error message on the “gMail.Send” line

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

Hi
Have you successfully:
_ Got 2 step verification on the account,
and then
_ successfully created (Generated) a 16 character Apps Password?
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

DocAElstein wrote:
15 Nov 2022, 21:06
Hi
Have you successfully:
_ Got 2 step verification on the account,
and then
_ successfully created (Generated) a 16 character Apps Password?
Yes I made all those changes to my selected Gmail and I used the 16 character Apps Password in my code.

Code: Select all

    Dim i As Long
    Dim fd As FileDialog
    Dim strTo As String
    Dim strSubject As String
    Dim strBody As String

    On Error GoTo eh:

    'create a CDO object
    Dim gMail As CDO.Message
    Set gMail = New CDO.Message

    'Enable SSL Authentication
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

    'Make SMTP authentication Enabled=true (1)
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

    'Set the SMTP server and port Details
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Set your username and App password for your own gmail account here
    gMail.Configuration.Fields.Item _
       ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "talent@homecareangelstx.com"
    gMail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "16CodeAppPassword"
        

    'Update the configuration fields
    gMail.Configuration.Fields.Update

    'set the email properties and the file to send
    With gMail
        .Subject = Sheets("Applicant Email").Range("A1").Value 'email subject
        .From = "talent@homecareangelstx.com"
        .To = Sheets("Applicant Email").Range("A2").Value 'email address
        .TextBody = "Hi " & Sheets("Applicant Email").Range("A2").Value & _
            vbNewLine & Sheets("Applicant Email").Range("A6").Value 'Email message
   End With

   'select the file(s) to send with the microsoft file dialog box
    Dim dlgFile As FileDialog
    Dim strItem As Variant
    Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
    dlgFile.AllowMultiSelect = True
    If dlgFile.Show Then
        For Each strItem In dlgFile.SelectedItems
            gMail.AddAttachment strItem
        Next strItem
    End If

    'send the mail
    gMail.Send
    MsgBox "Emails Sent"
    Exit Sub
eh:
    MsgBox "Email creation failed!"

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

Hi, OK, Thx
_._________________________-

I modified just slightly that macro to the one below ( I made no major changes, it is the same basic coding ). I used a spare gmail account of mine, and ran it.

Code: Select all

 '  Hans   https://eileenslounge.com/viewtopic.php?p=300925#p300925
'  https://eileenslounge.com/viewtopic.php?p=300922#p300922   -  set a reference to Microsoft CDO 1.21 Library, via Tools > References in the VBA editor.
Sub Eh()
Dim i As Long, strTo As String, strBody As String
'Dim fd As FileDialog
' On Error GoTo Eh:
'create a CDO object
Dim gMail As CDO.Message  '   For me I needed   Microsoft CDO for Windows 2000 Library
 Set gMail = New CDO.Message
'Enable SSL Authentication
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Set the SMTP server and port Details
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Set your username and App password for your own gmail account here
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "ASpareEmailOfMine@gmail.com"
gMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "abcdwxyzabcdwxyz"       '    16 character Apps Password
'Update the configuration fields
gMail.Configuration.Fields.Update
   'set the email properties and the file to send
    With gMail
     .Subject = "Subjectively being" ' ActiveSheet.Range("A1").Value    '    Sheets("Applicant Email").Range("A1").Value 'email subject
'        .From = """SendingFrom gmail"" "  '  "talent@homecareangelstx.com" '  gives error     Laufzeitehler '-2147220979' (8004020d)':                 At least one of the from or Sender fields is required, and neither was found
     .From = " ASpareEmailOfMine @gmail.com"
     .To = "doc.aelstein@gmail.com" 'email address
     .TextBody = "Hi, " '  & Sheets("Applicant Email").Range("A2").Value & vbNewLine & Sheets("Applicant Email").Range("A6").Value 'Email message
   End With
'   'select the file(s) to send with the microsoft file dialog box
'    Dim dlgFile As FileDialog
'    Dim strItem As Variant
'    Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)
'    dlgFile.AllowMultiSelect = True
'    If dlgFile.Show Then
'        For Each strItem In dlgFile.SelectedItems
'            gMail.AddAttachment strItem
'        Next strItem
'    End If
'
'    'send the mail
    gMail.Send
'    MsgBox "Emails Sent"
'    Exit Sub
'Eh:
'    MsgBox "Email creation failed!"
End Sub 
It worked , no problem:-
Arrived, Subjectively being.JPG
_.________________

Here is another version, more similar to the form what I typically use. It does not need any extra external library reference, (It uses an imaginary fantasy one I dreamt about once), but its basically the same coding as that from Hans. You could try that, but I don’t expect it will work any different for you.

Code: Select all

 Sub SendUsinggmailWithLCD_CW___Eh()
Rem  EMail send 'For info see:  http://www.excelfox.com/forum/showthread.php/2233-Urgent-support-needed-Multiple-emails-multiple-Excel-workbooks-at-once#post10519
'Working at my end With my With End With Created LCDCW Library, (LCD 1.0 Library ) (Linking Configuration Data_Cods Wollups)
    With CreateObject("CDO.Message") ' -Linking Cods Wollups--------
    Dim LCD_CW As String: Let LCD_CW = "http://schemas.microsoft.com/cdo/configuration/"
     .Configuration(LCD_CW & "smtpusessl") = True '
     .Configuration(LCD_CW & "smtpauthenticate") = 1  '
    '  ' Sever info
     .Configuration(LCD_CW & "smtpserver") = "smtp.gmail.com"
    '  The mechanism to use to send messages.
     .Configuration(LCD_CW & "sendusing") = 2
     .Configuration(LCD_CW & "smtpserverport") = 25
    '
     .Configuration(LCD_CW & "sendusername") = " ASpareEmailOfMine@gmail.com" '
   ' Apps password   https://www.excelfox.com/forum/showthread.php/2380-Tests-and-Notes-for-EMail-Threads?p=19039&viewfull=1#post19038      https://www.excelfox.com/forum/showthread.php/2380-Tests-and-Notes-for-EMail-Threads?p=19039&viewfull=1#post19039
     .Configuration(LCD_CW & "sendpassword") = "abcdwxyzabcdwxyz"  ' - This is the 16 character  so called  "App Pasword"   - You  Generate it,   but Google decides what characters it is  :-  
    ' Optional - How long to try
     .Configuration(LCD_CW & "smtpconnectiontimeout") = 30 '
    ' Intraction protocol is Set/ Updated
     .Configuration.Fields.Update '
    'End With ' ----------------------      my Created  LCDCW Library
    'With ' --- ' Data to be sent------     my Created  LCDCW Library
     Dim strHTML As String: Let strHTML = "<p>" & ActiveSheet.Range("B2").Value & "</p><p>How are you Today?</p>"
    .To = "doc.aelstein@gmail.com" '
    .From = """SendingFrom gmail"" <YourEMailAddresseOrAnyCrap>"
    .Subject = Range("A2").Value
    .htmlbody = strHTML
    '.AddAttachment ThisWorkbook.Path & "AnyFile.xyz"
    .Send ' Do it
    End With ' CreateObject("CDO.Message") -----my Created  LCDCW Library
End Sub 
That also works for me:-
Cods Wollops also arrived.JPG





_.____

I repeat my offer from a few posts back, - if you care to share with me the username and (normal) password of a spare Freemail gmail Account per Private message or Email, then I will see if I can get that working for you, then we can take it from there. If you do that then :
_ please tell me in what land or Part of the USA it was registered
_ bear in mind that when I try to use it you may get some:
security warnings;
or get sent some confirmation code;
or may be asked to confirm its you messing about with the gmail. Obviously it will be me not you messing with the gmail, but please tell them its you and send me quickly any confirmation codes they send you.
_ tell me ( per Private message or Email if you prefer ) what time and what days you are awake and near a computer, so that I do anything at that time, because you must always react quickly to any security warnings for gmail. ( Don’t forget to tell me what time zone you are on ( – as I post this, its about 10.32 in the morning, 16 November 2022 - German time zone ) )

_.___________________


Otherwise, I will post again if I have any other ideas or suggestions.



Alan
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Using Excel VBA to send emails from my Gmail account

Post by ABabeNChrist »

Hi DocAElstein
I created a new Gmail address and set all the perimeters as you have suggested. I also retested on my end, no success with same error message. I sent you a private message with email address and passwords

And yes I live in Texas

Thank you for you help

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Using Excel VBA to send emails from my Gmail account

Post by DocAElstein »

Hi
OK, Thx for the Private message.

Because you have created an App password on that account, ( an therefore presumably necessarily before turned on 2 – Step Verification ) , it will make things a little more difficult for us to work together as you will receive various extra security requests to verify things when I initially try to do things with the account.
So it would be easier for us initially if you remove the apps password and turn off 2 – Step verification.
(Once I am able to log in easily I can adjust the security settings and then re enable 2 – Step Verification and create app passwords as needed).

So a couple requests: 2 things to do please

_1) Remove the app password and turn off 2 – step verification. ( here some screenshots on doing that:
https://excelfox.com/forum/showthread.p ... #post19041
https://excelfox.com/forum/showthread.p ... #post19041 )


_2) Give me a time (Texas time) that you are able to respond quickly to any security requests from Google:
I can spend some time on the computer tomorrow, Friday 18 November.***
On Friday I will make an attempt initially to log in with that account. You will probably get some warnings and may be asked to verify in some way that it is you. ( Even with 2 – Step verification turned off, Google will still likely require some security confirmations etc., when anybody tries to use the account)
Remember that for now just say always Yes to everything, - confirm everything.
These confirmations must be done fairly quickly, or else Google will block the account.
So it will be helpful if you tell me a time tomorrow when you are certain you are logged in to a computer, and/ or can respond quickly to any request from Google per Email, SMS, phone call or whatever. (Exactly what happens will depend on what information or phone numbers or Email accounts you gave when creating that account).

I am available form 10.00 am in the morning until 10.00p.m in the evening German time.
So that will be I think your Texas time from 3.00am in the morning until 3.00 p.m. on the afternoon
( I think in Texas you are 7 Hours behind me in Germany )
There is no need for you to make yourself available very early in the morning. But perhaps you can tell me the earliest time on Friday***, Texas time, that it is convenient for you. I will then make my first attempt to log into the account at that time. If all goes well, you should only receive a couple of warnings to respond to shortly after that time. You may be sent some confirmation code, and you will need to send that to me quickly, either by private message or EMail to me.
I will pass on my private Email to you now via private message. That may help us to communicate tomorrow or whenever more quickly if needed.

( *** Or you suggest a later date that is convenient for you. )

Alan
Last edited by DocAElstein on 19 Nov 2022, 12:39, edited 2 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(