如何在没有 Outlook 的情况下通过 VBA 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41697695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:54:10  来源:igfitidea点击:

How to send e-mail through VBA without Outlook

vbaemailsmtp

提问by Fritsch

I'm trying to send email through SMTP in VBA, but is returning error.

我正在尝试通过 VBA 中的 SMTP 发送电子邮件,但返回错误。

Dim CDOmsg As CDO.Message
Set CDOmsg = New CDO.Message

With CDOmsg.Configuration.Fields
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypass"
    .Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    .Update
End With
' build email parts
With CDOmsg
    .Subject = "the email subject"
    .From = "[email protected]"
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .TextBody = "the full message body goes here. you may want to create a variable to hold the text"
End With
CDOmsg.Send
Set CDOmsg = Nothing

The error is on CDOmsg.Send. I've tried to send with Gmail and Yahoo Mail, but I get this same error.

错误在 CDOmsg.Send 上。我曾尝试使用 Gmail 和 Yahoo Mail 发送邮件,但我遇到了同样的错误。

Error code: -2147220973(80040213)

Error description: The transport failed to connect to the server

错误代码:-2147220973(80040213)

错误描述:传输连接服务器失败

回答by F.Kokert

You can try the following but don't forget to tick the checkbox for 'Microsoft CDO for Windows 2000 Library'

您可以尝试以下操作,但不要忘记勾选“Microsoft CDO for Windows 2000 Library”复选框

Function email(ByVal sender_email As String, _
            ByVal email_message As String, _
            ByVal email_message2 As String, _
            ByVal reply_address As String, _
            ByVal sender_name As String)       

    Dim Mail As New Message

    Dim Cfg As Configuration

    Set Cfg = Mail.Configuration

    'SETUP MAIL CONFIGURATION FIELDS
    Cfg(cdoSendUsingMethod) = cdoSendUsingPort
    Cfg(cdoSMTPServer) = 'SMTP
    Cfg(cdoSMTPServerPort) = 'SMTPport
    Cfg(cdoSMTPAuthenticate) = cdoBasic
    Cfg(cdoSMTPUseSSL) = True
    Cfg(cdoSendUserName) = 'sender_email
    Cfg(cdoSendPassword) = 'password
    Cfg.Fields.Update

    'SEND EMAIL
    With Mail
        .From = 'sender_name & sender_email
        .ReplyTo = 'reply_address
        .To = 'receiver
        .CC = 'carbonCopy
        .BCC = 'blindCopy
        .Subject = 'SubjectLine
        .HTMLBody = 'email_message & email_message2 
        .Attachments.Add attFilePath
        .Send
    End With