79350295

Date: 2025-01-12 16:52:45
Score: 1
Natty:
Report link

So, In case anyone of the contributors failed to see my comments - thank you all very much.

By taking the hints from BobS and the coding of Harun24hr I got everything to work perfectly. My final code is here:

Sub Send_Email()
' Create error handler
On Error GoTo errHandle

' Create the new instances of the objects
Set newMail = New CDO.Message
Set newConfiguration = New CDO.Configuration

' Set all the default values
newConfiguration.Load -1

' Put in the message info
With newMail
    .Subject = "VBA Test"
    .From = "********@gmail.com"
    .To = "********@outlook.com"
    .BCC = "******@outlook.com;*******@t********y.ltd.uk"
    .TextBody = "Test message sent using VBA script in Access"
End With

' Make the Fields
Set Fields = newConfiguration.Fields

With Fields
    .Item(cdoSendUserName) = "*********"
    .Item(cdoSendPassword) = "dk** **** **** **wy"     ' App password
    .Item(cdoSMTPUseSSL) = True
    .Item(cdoSMTPAuthenticate) = 1
    .Item(cdoSMTPServer) = "smtp.gmail.com"
    .Item(cdoSMTPServerPort) = 465
    .Item(cdoSendUsingMethod) = 2

    ' Update the configuration
  .Update
    
End With
' Transfer the configuration
newMail.Configuration = newConfiguration

' Send the email
newMail.Send

MsgBox "Email has been sent", vbInformation

' Exit lines for routine
exit_line:
    ' Release object from memory
    Set newMail = Nothing
    Set newConfiguration = Nothing
    
    Exit Sub
    
' Error handling
errHandle:
Select Case Err.Number
    Case -2147220973  'Could be because of Internet Connection
        MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description
    Case -2147220975  'Incorrect credentials User ID or password
        MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description
    Case Else   'Report other errors
        MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.Description
    End Select

    Resume exit_line

End Sub

In practice the values for message from, to, subject, etc, etc would be picked up from an associated from that pulls email addresses from a data file using an SQL query.

This has also been tested and works.

Many thanks again to one and all and i hope my solution may be of use to others.

Reasons:
  • Blacklisted phrase (0.5): thank you
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Trev Buck