VBA使用Gmail发送电子邮件:传输连接失败

VBA使用Gmail发送电子邮件:传输连接失败,vba,excel,gmail,cdo.message,Vba,Excel,Gmail,Cdo.message,运行下面的Excel VBA代码时出现以下错误: 运行时错误-2147220973 传输无法连接到服务器 现在我使用下面的代码,它工作得很好。 别忘了更改你的gmail配置以允许从其他应用发送电子邮件 Sub sendEmail(gmail, password, targetEmail, subject, bodyContent) Dim Mail As New Message Dim Config As Configuration: Set Config = Mail.Con

运行下面的Excel VBA代码时出现以下错误:

运行时错误-2147220973
传输无法连接到服务器


现在我使用下面的代码,它工作得很好。 别忘了更改你的gmail配置以允许从其他应用发送电子邮件

Sub sendEmail(gmail, password, targetEmail, subject, bodyContent)

    Dim Mail As New Message
    Dim Config As Configuration: Set Config = Mail.Configuration

    Config(cdoSendUsingMethod) = cdoSendUsingPort
    Config(cdoSMTPServer) = "smtp.gmail.com"
    Config(cdoSMTPServerPort) = 465
    Config(cdoSMTPAuthenticate) = cdoBasic
    Config(cdoSMTPUseSSL) = True
    Config(cdoSendUserName) = gmail
    Config(cdoSendPassword) = password
    Config.Fields.Update

    Mail.To = targetEmail
    Mail.from = Config(cdoSendUserName)
    Mail.subject = subject
    Mail.HTMLBody = bodyContent

    Mail.Send

End Sub

现在我使用下面的代码,它工作得很好。 别忘了更改你的gmail配置以允许从其他应用发送电子邮件

Sub sendEmail(gmail, password, targetEmail, subject, bodyContent)

    Dim Mail As New Message
    Dim Config As Configuration: Set Config = Mail.Configuration

    Config(cdoSendUsingMethod) = cdoSendUsingPort
    Config(cdoSMTPServer) = "smtp.gmail.com"
    Config(cdoSMTPServerPort) = 465
    Config(cdoSMTPAuthenticate) = cdoBasic
    Config(cdoSMTPUseSSL) = True
    Config(cdoSendUserName) = gmail
    Config(cdoSendPassword) = password
    Config.Fields.Update

    Mail.To = targetEmail
    Mail.from = Config(cdoSendUserName)
    Mail.subject = subject
    Mail.HTMLBody = bodyContent

    Mail.Send

End Sub
故障源 导致此混乱的打字错误-smptserverport应该是smtpserverport

我试过的东西 在解决这个问题的路上,我尝试了很多事情。但现在我不确定是否真的需要这些。 不过,有些人可能会从列表中获得一些东西:

  • 启用“”
  • 在测试连接之前打开“”
  • 确保您有一个强密码
  • 不再推荐
  • 不是必须的,但强烈推荐
几点提示:
  • 明文发送密码时,应使用端口465
  • 发送安全密码时应使用端口587
如果您需要在VBA/VBS中使用安全密码,可以在PowerShell()中使用以下小技巧:

故障源 导致此混乱的打字错误-smptserverport应该是smtpserverport

我试过的东西 在解决这个问题的路上,我尝试了很多事情。但现在我不确定是否真的需要这些。 不过,有些人可能会从列表中获得一些东西:

  • 启用“”
  • 在测试连接之前打开“”
  • 确保您有一个强密码
  • 不再推荐
  • 不是必须的,但强烈推荐
几点提示:
  • 明文发送密码时,应使用端口465
  • 发送安全密码时应使用端口587
如果您需要在VBA/VBS中使用安全密码,可以在PowerShell()中使用以下小技巧:


尝试使用端口587而不是465它不起作用尝试使用端口587而不是465它不起作用
Dim myPassword, cmd, shell, executor, securePassword
myPassword = "ABCABCABC....."
cmd = "powershell.exe ConvertTo-SecureString "& myPassword
Set shell = CreateObject("WScript.Shell")
Set executor = shell.Exec(cmd)
executor.StdIn.Close
securePassword = executor.StdOut.ReadAll