Hi,
I have a code to automatically send e-mails via gmail, but I need to attach a couple of files that are in a specific folder.I tryed code it, but didn´t work.
I can´t make a code with a specific name for the files, because every moth when we send the e-mails, they are with different names. The only thing that never changes is the location that the files are in the computor, as they are specified in the sheet (example bellow)
Can anyone help me??
Sub Send_Emails()
For Line = 2 To 200
Dim NewMail As CDO.Message
Dim mailConfig As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo Err:
'early binding
Set NewMail = New CDO.Message
Set mailConfig = New CDO.Configuration
'load all default configurations
mailConfig.Load -1
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.From = ""
.To = Cells(Line, 2).Value & "," & Cells(Line, 3).Value & "," & Cells(Line, 4).Value & "," & Cells(Line, 5).Value
.CC = ""
.BCC = ""
.Subject = "Faturas do mês"
.TextBody = "Segue em anexo planilha do mês"
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") = "" 'Your gmail address
.Item(msConfigURL & "/sendpassword") = "" 'Your password or App Password
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.Send
Cells(Line, 8) = "Enviado"
Next
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
Resume Exit_Err
End Sub
I have a code to automatically send e-mails via gmail, but I need to attach a couple of files that are in a specific folder.I tryed code it, but didn´t work.
I can´t make a code with a specific name for the files, because every moth when we send the e-mails, they are with different names. The only thing that never changes is the location that the files are in the computor, as they are specified in the sheet (example bellow)
Can anyone help me??
Seller | e-mail1 | email2 | email3 | email4 | Body | Folder | Status |
Thiago Goncalves | danilogon@gmail.com | Segue em anexo o relatorio de vendas | C:\Users\Admin\Desktop\RELATORIOS\Thiago | ||||
Danilo Goncalves | danilogon2@gmail.com | Segue em anexo o relatorio de vendas | C:\Users\Admin\Desktop\RELATORIOS\Danilo |
Sub Send_Emails()
For Line = 2 To 200
Dim NewMail As CDO.Message
Dim mailConfig As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo Err:
'early binding
Set NewMail = New CDO.Message
Set mailConfig = New CDO.Configuration
'load all default configurations
mailConfig.Load -1
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.From = ""
.To = Cells(Line, 2).Value & "," & Cells(Line, 3).Value & "," & Cells(Line, 4).Value & "," & Cells(Line, 5).Value
.CC = ""
.BCC = ""
.Subject = "Faturas do mês"
.TextBody = "Segue em anexo planilha do mês"
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") = "" 'Your gmail address
.Item(msConfigURL & "/sendpassword") = "" 'Your password or App Password
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.Send
Cells(Line, 8) = "Enviado"
Next
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
Resume Exit_Err
End Sub