Public Sub CDO_Email()
On Error GoTo Err_ErrorHandler
Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
gblnEmailFailed = False
Set CDO_Mail = CreateObject("CDO.Message")
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = gstrUsing
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = gstrServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = gstrAuthenticate
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = gstrFrom
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = gstrPassword
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = gstrPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = gstrUseSSL
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = gintTimeOut
'code for STARTTLS
If txtPort = 587 Then
.Item("http://schemas.microsoft.com/cdo/configuration/sendtls").Value = True
End If
.Update
End With
With CDO_Mail
Set .Configuration = CDO_Config
End With
With CDO_Mail
.Subject = gstrSubject
.From = gstrFrom
.To = gstrTo
.TextBody = gstrBody
If Len(gstrAttachment) > 0 Then
.AddAttachment (gstrAttachment)
End If
.Send
End With
Set SMTP_Config = Nothing
Set CDO_Mail = Nothing
Exit_ErrorHandler:
'Access 2007 Developer Reference > Microsoft Data Access Objects (DAO) Reference > DAO Reference > Recordset Object > Methods
'An alternative to the Close method is to set the value of an object variable to Nothing (Set dbsTemp = Nothing).
Exit Sub
Err_ErrorHandler:
If Err.Number <> 0 Then
Select Case Err.Number
Case -2147220977 'Likely cause, Incorrectly Formatted Email Address, server rejected the Email Format
MsgBox "Error From --- fSendGmail --- Incorrectly Formatted Email --- Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , "Format the Email Address Correctly"
Case -2147220980 'Likely cause, No Recipient Provided (No Email Address)
MsgBox "Error From --- fSendGmail --- No Email Address --- Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , "You Need to Provide an Email Address"
Case -2147220960 'Likely cause, SendUsing Configuration Error
MsgBox "Error From --- fSendGmail --- The SendUsing configuration value is invalid --- LOOK HERE >>> sendusing) = conCdoSendUsingPort --- Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , "SendUsing Configuration Error"
Case -2147220973 'Likely cause, No Internet Connection
MsgBox "Error From --- fSendGmail --- No Internet Connection --- Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , "No Internet Connection"
Case -2147220975 'Likely cause, Incorrect Password
MsgBox "Error From --- fSendGmail --- Incorrect Password --- Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , "Incorrect Password"
Case Else 'Report Other Errors
MsgBox "Error From --- fSendGmail --- Error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description
End Select
gblnEmailFailed = True
Resume Exit_ErrorHandler
End If
End Sub