The transport failed to connect to the server.

abderrahim1234 Posted messages 6 Status Member -  
jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   -
Hello,
I am creating a VBA application that sends mail from an application, and it gives me

Runtime error - 2147220973 (80040213) The transport failed to connect to the server.

Please help if anyone knows the solution.

Sub send_Gmail()

Dim NewMail As CDO.Message
Dim mailConfiguration As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String

On Error GoTo errHandle

Set NewMail = New CDO.Message
Set mailConfiguration = New CDO.Configuration

mailConfiguration.Load -1

Set fields = mailConfiguration.fields

With NewMail
.Subject = "hello"
.From = "********.fr"
.To = "*****@*****.fr"
.CC = ""
.BCC = ""
.TextBody = "This is a test email."
.AddAttachment "C:\Users\MonImageExcel.jpg"
End With

With fields
.Item("http://schemas.microsoft.com/cdo/configuration/urlproxyserver") = "proxy.server:8080"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "****@**.fr"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "******"

.Update

End With

NewMail.Configuration = mailConfiguration
NewMail.Send

MsgBox "E-Mail has been sent", vbInformation

exit_line:
'// Release object memory
Set NewMail = Nothing
Set mailConfiguration = Nothing

Exit Sub

errHandle:

MsgBox "Error: " & Err.Description, vbInformation

GoTo exit_line

End Sub

1 answer

  1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 832
     
    Hello,

    In the future, please use CODE TAGS.

    Here is a code that should work
     Public Function send_email() Set cdomsg = CreateObject("CDO.message") With cdomsg.Configuration.Fields .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" .Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587 .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mygmail@gmail.com" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword" .Update End With ' build email parts With cdomsg .To = "somebody@somedomain.com" .From = "mygmail@gmail.com" .Subject = "the email subject" .TextBody = "the full message body goes here. you may want to create a variable to hold the text" .Send End With Set cdomsg = Nothing End Function 


    --
    Best regards,
    Jordane
    0
    1. Momen
       
      Thank you Jordane.

      I want to try, but it doesn't work in my code.
      0
      1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 832 > Momen
         
        Have you set up your Google account to allow external applications?
        Did you use your Gmail password, or, as recommended, an app password?
        Check this out: https://support.google.com/accounts/answer/185833
        0