Gmail problem (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
I have the following code that worked fine until I tried it today. I have not used it in a while, so I don't know how long this has been not working properly.

Code:
 msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
'Enable SSL Authentication
.Item(msConfigURL & "/smtpusessl") = True

'Make SMTP authentication Enabled=true (1)
.Item(msConfigURL & "/smtpauthenticate") = 1
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
.Item(msConfigURL & "/smtpserverport") = 465
.Item(msConfigURL & "/sendusing") = 2
.Item(msConfigURL & "/sendusername") = "mySignInName@gmail.com"
.Item(msConfigURL & "/sendpassword") = "myPassword"
.Update

End With
NewMail.Configuration = mailConfig

NewMail.Send
Now I get the following error message:

"Incorrect Credentials!! – This message could not be sent to the SMPT server. The transport error code was 0x80040217. The server response was not available."



Thinking perhaps I needed to be on Gmail to use this, I logged on to my account but get the same message.
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
Thank you both for your suggestions on this. I set the Gmail setting as was suggested but still get the same error message. Also got about a dozen message from Gmail telling me that I had committed a grave error in setting to less secured app. I don't want to bet this old horse to death, but is it possible to amend the above code to work or would it behoove me to use Yahoo mail instead? And if the latter, what settings in my code might be different aside from the email address an pass code? Thanks.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:57
Joined
Oct 29, 2018
Messages
21,358
Thank you both for your suggestions on this. I set the Gmail setting as was suggested but still get the same error message. Also got about a dozen message from Gmail telling me that I had committed a grave error in setting to less secured app. I don't want to bet this old horse to death, but is it possible to amend the above code to work or would it behoove me to use Yahoo mail instead? And if the latter, what settings in my code might be different aside from the email address an pass code? Thanks.
Hi. If you use Yahoo!, you would have to get their SMTP settings from them. In reality, though, you're basically using an outdated technology. You could consider using a third-party app or use Google's API and use OAuth, so you don't have to worry about using a less secured app.

Just my 2 cents...
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
Thank you for your response. I looked at the Google's API and use OAuth suggestion and I have to admit this is way beyond my area of expertise and knowledge of coding, Google and etc. So I will either have to use the Outlook mail which is a bad alternative for me for security reasons or Yahoo or some other email application.

Just trying to keep things simple.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:57
Joined
Oct 29, 2018
Messages
21,358
Thank you for your response. I looked at the Google's API and use OAuth suggestion and I have to admit this is way beyond my area of expertise and knowledge of coding, Google and etc. So I will either have to use the Outlook mail which is a bad alternative for me for security reasons or Yahoo or some other email application.

Just trying to keep things simple.
Hi. It may not be so much as the email application as it may be the email server. Maybe CDO will will work with Yahoo! or Outlook.com, I just don't know.

You might also try to search for third-party apps for GMail.
 

isladogs

MVP / VIP
Local time
Today, 04:57
Joined
Jan 14, 2017
Messages
18,186
@Eljefegeneo
Did you look at the Help file I mentioned in post #3?
I ask as there are two ways of getting GMail to work with CDO (or any external app).

The quote below is from the relevant section of the help file:

There are two possible solutions both of which require a change in the security section of your Google account settings:
a) allow less secure apps - if this is applied, GMail will again work via CDO.
However, this is not available if you have switched on two-step authentication

b) set app password = enter a name for the app e.g. CDO EMail Tester and it will generate a unique password for that app and your account.
Enter that in place of your usual password & CDO email for your GMail account will again work perfectly!

See these links for more info:
https://support.google.com/accounts/answer/6010255?hl=en&ref_topic=7188673#
https://support.google.com/accounts/answer/185833?hl=en

I ALWAYS use method b) - set app password. It definitely works and is completely secure
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:57
Joined
Sep 21, 2011
Messages
14,048
Yahoo has the same issue, and I recall I had to set an app password for that as well?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:57
Joined
May 7, 2009
Messages
19,169
do we have same code?
this code will work if you "Enable Less Secured App" in you GMail.
Code:
Private Const cdoSendUsingPort = 2
Private Const cdoBasic = 1
Private Const cdoNTLM = 2

' The docmentation of all these settings is available at
' https://msdn.microsoft.com/en-us/library/ms872853.aspx

Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Private Const cdoSMTPUseSSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"

Public Sub SendSimpleCDOMailWithAuthenticationAndEncryption()
    
    Dim mail    As Object           ' CDO.MESSAGE
    Dim config  As Object           ' CDO.Configuration
    
    Set mail = CreateObject("CDO.Message")
    Set config = CreateObject("CDO.Configuration")
    
    config.fields(cdoSendUsingMethod).Value = cdoSendUsingPort
    config.fields(cdoSMTPServer).Value = "smtp.gmail.com"
   
    config.fields(cdoSMTPServerPort).Value = 465  ' implicit SSL - Does not work with Explicit SSL (StartTLS) usually on Port 587
    config.fields(cdoSMTPUseSSL).Value = "true"
    
    config.fields(cdoSMTPAuthenticate).Value = cdoBasic
    config.fields(cdoSendUserName).Value = "your_email@gmail.com"
    config.fields(cdoSendPassword).Value = "your_email_password"

    config.fields.Update
    
    Set mail.Configuration = config
    
    With mail
        .To = "receiver_email"
        .From = "your_email@gmail.com"
        .Subject = "CDO test"
        .Textbody = "This is the body of the first plain text email with CDO."
        
        '.AddAttachment "C:\path\to\a\file.dat"
        
        .Send
    End With
    
    Set config = Nothing
    Set mail = Nothing
    
End Sub
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
No, mine slightly different. I tried this but as I am using late binding many lines of the above code say that I have not defined.
These are:
Code:
  config.fields(cdoSendUsingMethod).Value = cdoSendUsingPort
  config.fields(cdoSMTPServer).Value = "smtp.gmail.com"
   
   config.fields(cdoSMTPServerPort).Value = 465  ' implicit SSL - Does not work with Explicit SSL (StartTLS) usually on Port 587
   config.fields(cdoSMTPUseSSL).Value = "true"
    
   config.fields(cdoSMTPAuthenticate).Value = cdoBasic
   config.fields(cdoSendUserName).Value = "your_email@gmail.com"
   config.fields(cdoSendPassword).Value = "your_email_password"

I have been using this:
Code:
 Set NewMail = CreateObject("CDO.Message")
        Set mailConfig = CreateObject("CDO.Configuration")
            mailConfig.Load -1
                Set fields = mailConfig.fields

    With NewMail
        .Subject = "Email Automation Gmail Main DB"
        .From = "username@gmail.com"
        .To = "someone@yahoo.com"
                .Textbody = "This is a copy of the Back End of the Main DB"
        .AddAttachment fAttach
    End With

    msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

    With fields
        'Enable SSL Authentication
        .Item(msConfigURL & "/smtpusessl") = True

        'Make SMTP authentication Enabled=true (1)
        .Item(msConfigURL & "/smtpauthenticate") = 1
        .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
        .Item(msConfigURL & "/smtpserverport") = 587
        .Item(msConfigURL & "/sendusing") = 2 
        .Item(msConfigURL & "/sendusername") = "someone@yahoo.com"
        .Item(msConfigURL & "/sendpassword") = "password"
        .Update

    End With
    NewMail.Configuration = mailConfig
   
    NewMail.Send
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:57
Joined
May 7, 2009
Messages
19,169
the code is using Late binding.
some text need to be supplied by you like your_email, your_password, receiver_email.
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
I am getting the error message, Compile Error, Variable not Defined as follows: (shown in red)

config.fields(cdoSendUsingMethod).Value = cdoSendUsingPort

config.fields(cdoSMTPServer).Value = "smtp.gmail.com"

config.fields(cdoSMTPServerPort).Value = 465

config.fields(cdoSMTPUseSSL).Value = "true"

config.fields(cdoSMTPAuthenticate).Value = cdoBasic

config.fields(cdoSendUserName).Value = your_email@gmail.com

config.fields(cdoSendPassword).Value = "your_email_password"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:57
Joined
May 7, 2009
Messages
19,169
add the Private Constant Variables to your Module:
Code:
Private Const cdoSendUsingPort = 2
Private Const cdoBasic = 1
Private Const cdoNTLM = 2

' The docmentation of all these settings is available at
' https://msdn.microsoft.com/en-us/library/ms872853.aspx

Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Private Const cdoSMTPUseSSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 21:57
Joined
Jan 10, 2011
Messages
904
Thank you. However when I debug, the term Private Const throws this error message: "Compile Error, Invalid Attribute in Sub or Function"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:57
Joined
May 7, 2009
Messages
19,169
put it Outside of any sub/function.
put it After:

Option Compare Database
Option Explicit

Private Const cdoSendUsingPort = 2
Private Const cdoBasic = 1
Private Const cdoNTLM = 2

' The docmentation of all these settings is available at
' https://msdn.microsoft.com/en-us/library/ms872853.aspx

Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Private Const cdoSMTPUseSSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword
 

Users who are viewing this thread

Top Bottom