When sending mail via SMTP, I get “Transport Faile

2019-08-03 09:58发布

问题:

I have the code below to send mail from a VBA macro using CDO. I get an error in the code:

Transport failed To connect to server Error

I am sending a mail from the Gmail SMTP service. Looks like the configuration is set correctly, but somehow it doesn't work.

Sub Email()

    Dim CDO_Mail As Object
    Dim CDO_Config As Object
    Dim SMTP_Config As Variant
    Dim strSubject As String
    Dim strFrom As String
    Dim strTo As String
    Dim strCc As String
    Dim strBcc As String
    Dim strBody As String

    strSubject = "Results from Excel Spreadsheet"
    strFrom = "xxx@gmail.com"
    strTo = "xxx@gmail.com"
    strBody = "The total results are: 167"

    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") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxx@gmail.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Update
    End With

    With CDO_Mail
        Set .Configuration = CDO_Config
    End With

    CDO_Mail.Subject = strSubject
    CDO_Mail.From = strFrom
    CDO_Mail.To = strTo
    CDO_Mail.TextBody = strBody
    CDO_Mail.Send

End Sub

回答1:

That code works totally fine for me (send from Gmail to Gmail) - so you need to check the following:

  • try it with port 587 as well as port 465 (further reading)
  • configure your sending account in Gmail for Access for less secure apps - there is a Turn On option per the following support page

Allowing less secure apps to access your account Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.

Some examples of apps that do not support the latest security standards include:

The Mail app on your iPhone or iPad with iOS 6 or below

The Mail app on your Windows phone preceding the 8.1 release

Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird

...

Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:

Go to the "Less secure apps" section in My Account.

Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

CDO is pretty old now so assume that is an example of an app that doesn't support latest security standards.