Shloime
New Member
- Joined
- Oct 25, 2023
- Messages
- 48
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
I have the following code it does not work how can i get it work for using to send from a gmail
VBA Code:
'Build the text for the body of the message (could be read from ranges in Excel)
mlText = "Thanks for buying from us" & vbNewLine & _
"Please find you invoice attached."
On Error GoTo Err:
'early binding
'Set NewMail = New CDO.Message
'Set mailConfig = New CDO.Configuration
Set NewMail = CreateObject("CDO.Message")
Set mailConfig = NewMail.Configuration
'load all default configurations
mailConfig.Load -1
EML = "**************@gmail.com"
PAS = "Password"
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.From = EML
.To = "hellomyfriend@gmail.com"
.cc = ""
.BCC = ""
.Subject = "Ithe subject"
.TextBody = mlText ' & Str(Sheet1.Cells(2, 1))
.Addattachment "c:\data\email.xlsx" 'Optional file attachment; remove if not needed.
'.Addattachment "c:\data\email.pdf" 'Duplicate the line for a second attachment.
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.Item(msConfigURL & "/smtpusessl") = True 'Enable SSL Authentication
.Item(msConfigURL & "/smtpauthenticate") = 1 'SMTP authentication Enabled
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
.Item(msConfigURL & "/smtpserverport") = 465 'Set the SMTP port Details
.Item(msConfigURL & "/sendusing") = 2 'Send using default setting
.Item(msConfigURL & "/sendusername") = EML 'Your gmail address
.Item(msConfigURL & "/sendpassword") = PAS '"fprmkutzyopfalft" 'Your password or App Password"
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.Send
MsgBox "Your email has been sent", vbInformation
Exit_Err:
'Release object memory
Set NewMail = Nothing
Set mailConfig = Nothing
End
Err:
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
Last edited by a moderator: