"传输无法连接到服务器"尝试通过SMTP发送电子表格时

时间:2015-09-22 16:51:49

标签: excel vba email smtp

我尝试使用下面显示的代码通过SMTP发送Excel电子表格。但是,每当我运行代码时,都会收到错误"传输无法连接到服务器"

以下是代码:

    Sub CommandButton_Click()
Dim CDO_Mail_Object As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
Dim Email_Subject, Email_Send_From, Email_Send_To, Email_Cc, Email_Bcc, Email_Body As String

Email_Subject = "Trying to send email using CDO"
Email_Send_From = "xxxxx@yahoo.com"
Email_Send_To = "xxxxx@hotmail.com"
Email_Cc = ""
Email_Bcc = ""
Email_Body = "Congratulations!!!! You have successfully sent an e-mail using CDO !!!!"

Set CDO_Mail_Object = CreateObject("CDO.Message")

On Error GoTo debugs
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
'please put your server name below
.Item("http://schemas.Microsoft.Com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.Microsoft.Com/cdo/configuration/smtpusetls") = True
.Item("http://schemas.Microsoft.Com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxx@yahoo.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mail.yahoo.co.uk"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Update
End With

With CDO_Mail_Object
Set .Configuration = CDO_Config
End With

CDO_Mail_Object.Subject = Email_Subject
CDO_Mail_Object.From = Email_Send_From
CDO_Mail_Object.To = Email_Send_To
CDO_Mail_Object.TextBody = Email_Body
CDO_Mail_Object.cc = Email_Cc 'Use if needed
CDO_Mail_Object.BCC = Email_Bcc 'Use if needed
'CDO_Mail_Object.AddAttachment FileToAttach 'Use if needed
CDO_Mail_Object.send

debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub

上述代码中是否有错误(我已启用Excel中包含的Microsoft CDO库&gt;工具&gt;参考)或我提供的SMTP凭据中是否有错误?
我使用的是错误的Yahoo SMTP服务器(我应该使用.com)吗?

1 个答案:

答案 0 :(得分:0)

Dunno如果你的情况有效,但我最近在类似的帖子中看到了这一点。

尝试删除架构网址中的大写字母?

&#34; Microsoft.Com&#34; - &GT; &#34; microsoft.com&#34;