2017-01-04 101 views
1

我有下面的代碼使用CDO從VBA宏發送郵件。我在代碼中得到一個錯誤:SMTP VBA:發送郵件時 - 傳輸失敗連接到服務器

交通運輸無法連接到服務器時出現錯誤

我從Gmail的SMTP服務發送郵件。看起來像配置設置正確,但不知何故它不起作用。

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 = "[email protected]" 
    strTo = "[email protected]" 
    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") = "[email protected]" 
     .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 

回答

0

該代碼工作對我來說完全罰款(從發送的Gmail 的Gmail) - 所以你需要檢查以下內容:

  • 與端口587和端口465試試吧(further reading
  • 配置您的Gmail帳戶發送的Access for less secure apps - 有每個以下support page
  • 一個 Turn On選項

允許不夠安全的應用訪問您的帳戶 谷歌可能會阻止某些應用程序或設備不使用最新安全標準的登錄嘗試。由於這些應用和設備更容易侵入,阻止這些應用和設備有助於保持帳戶安全。

不支持最新的安全標準的應用程序的一些例子包括:

在您的iPhone或iPad上的郵件應用程序與iOS 6以下

的郵件應用程式Windows手機的8.1版本之前的上

一些桌面郵件客戶端如Microsoft Outlook和Mozilla Thunderbird

...

選項2:更改您的設置以允許安全性較低的應用訪問您的帳戶。我們不建議使用此選項,因爲這可能會讓別人更容易侵入您的帳戶。如果您想允許訪問,請按照以下步驟操作:

轉到我的帳戶中的「安全性較低的應用」部分。

在「訪問不太安全的應用程序」旁邊,選擇打開。 (請注意,以谷歌Apps用戶:如果您的管理員已鎖定不夠安全的應用帳戶訪問該設置是隱藏的。)

CDO是很老現在這麼假設是不支持應用程序的例子最新的安全標準

+0

謝謝你的工作。我選擇了選項2. :) – Harshil

+0

很高興提供幫助 - 如果您有答案,請隨時致電:) –

相關問題