使用Excel

时间:2017-03-20 08:56:50

标签: excel vba excel-vba email smtp

我正在尝试设置 .xlsm 工作簿,以使用电子表格中的特定数据向不同的电子邮件地址发送电子邮件。

我希望使用哪个电子邮件客户端或服务器并不重要。

我目前正试图让它适用于hotmail。

这是我的代码:

Sub Button1_Click()
    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 = "SUBJECT"
    strFrom = "******@hotmail.com"
    strTo = "************@hotmail.com"
    strCc = ""
    strBcc = ""
    strBody = "BODY TEXT HERE"

    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.live.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "******@hotmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .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.CC = strCc
    CDO_Mail.BCC = strBcc
    CDO_Mail.Send

    MsgBox ("Emails have been sent.")
End Sub

我收到以下错误:

  

运行时错误'-2147220975(80040211)'

     

无法将邮件发送到SMTP服务器,传输错误代码为0x80040217。服务器响应不可用。

我也尝试将端口从25更改为587,我收到错误:

  

传输无法连接到服务器。

我最初也没有以下两行:

.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

当他们不在那里时我得到了错误:

  

服务器拒绝了发件人地址。服务器响应为:530 5.7.0必须首先发出STARTTLS命令。

0 个答案:

没有答案