如何在没有Outlook的情况下通过VBA发送电子邮件 [英] How to send e-mail through VBA without Outlook

查看:576
本文介绍了如何在没有Outlook的情况下通过VBA发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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") = "myemail@gmail.com"
    .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 = "myemail@gmail.com"
    .To = "mailto@gmail.com"
    .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

错误在CDOmsg。发送。我曾尝试通过Gmail和Yahoo Mail发送邮件,但遇到同样的错误。

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


错误代码:-2147220973(80040213)

Error code: -2147220973(80040213)

错误描述:传输无法连接到服务器

Error description: The transport failed to connect to the server


推荐答案

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

You can try the following but don't forget to tick the checkbox for '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        

这篇关于如何在没有Outlook的情况下通过VBA发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆