微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试从 Excel 通过 Gmail 发送电子邮件时出现“发送”值错误

如何解决尝试从 Excel 通过 Gmail 发送电子邮件时出现“发送”值错误

我复制了一些代码以通过 Gmail 从 Excel 发送电子邮件并不断收到相同的错误错误:sendusing 配置值无效。 -2147220960

我看过这个问题的其他答案,这些答案对其他人有效,但对我无效。这可能是因为我在 Gmail 中使用了 2 阶段身份验证? 如果是这样,有没有办法解决这个问题?如果没有,任何人都可以提出前进的道路吗?我对基本的 VBA 相当有能力,但在这方面不擅长。

代码是:

Option Explicit

Sub Emailer()

Dim newMail As cdo.message
Dim mailconfiguration As CDO.Configuration
Dim msConfigURL As String
Dim fields As Variant

On Error GoTo errHandler

Set newMail = New cdo.message
Set mailconfiguration = New CDO.Configuration
mailconfiguration.Load -1
Set fields = mailconfiguration.fields

With newMail
    .Subject = "Odd Sock Cottage Bookings"
    .From = "dnwigg@gmail.com"
    .To = "dnwigg@gmail.com"
    .CC = ""
    .BCC = ""
    .TextBody = "Here's the latest booking list."
    .AddAttachment "C:\Users\David\Desktop\Odd Sock Cottage\Visitor instructions\Odd Sock Cottage Location and Parking COVID-19.pdf"
End With

msConfigURL = "http://schemas.microsoft.com/cdoCconfiguration"

With fields
    .Item(msConfigURL & "/smtpusessl") = 1
    .Item(msConfigURL & "smtpauthenticate") = 2
    .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
    .Item(msConfigURL & "/smtpserverport") = 465
    .Item(msConfigURL & "/sendusing") = 2
    .Item(msConfigURL & "/sendusername") = "dnwigg@gmail.com"
    .Item(msConfigURL & "/sendpassword") = "password"
    .Update
End With

newMail.Configuration = mailconfiguration
newMail.Send

MsgBox "Email sent.",vbinformation

exit_line:

    Set newMail = nothing
    Set mailconfiguration = nothing
    
Exit Sub

errHandler:

    MsgBox "Error: " & Err.Description & Err,vbinformation
    
    GoTo exit_line
Exit Sub
    
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。