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

时间:2021-05-26 11:28:04

标签: 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

0 个答案:

没有答案