Private Sub CmdSendMail_Click()
Dim mail As Object ' CDO.MESSAGE
Dim config As Object ' CDO.Configuration
Dim xFileToOpen As String
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 = "xxxxxxxx@xxxx.com"
config.fields(cdoSendPassword).Value = "xxxxxxxxx"
config.fields.Update
Set mail.Configuration = config
On Error Resume Next
ChDrive "M:"
ChDir "M:\xxxxxxx\xxxxxxx"
xFileToOpen = Application.GetOpenFilename(FileFilter:="PDF files,*.pdf", Title:="Select the right attachment")
With mail
.To = "xxxxx@xxxxxxxxxx.xxx"
.From = "xxxxxxx@xxxxx.xxx"
.Subject = "First email with CDO"
.TextBody = "This is the body of the first plain text email with CDO."
If xFileToOpen <> "" Then Add.Attachments xFileToOpen, 1
.Send
End With
On Error GoTo 0
Set config = Nothing
Set mail = Nothing
Unload Me 'delete userform
End Sub