通过Excel发送电子邮件

时间:2019-12-11 19:58:29

标签: excel vba gmail

我有一个要分发的项目。 我希望它在出现错误时自动向我发送邮件。

我在这里尝试使用CDO库,但是我不确定它是最佳使用方法。

我有:

Private Sub MyErrorHandler()
    'Here i save the file to send it next
    Dim path As String: path = Environ("AppData") + "\Local"
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs path & "AI_ErroringFile.xls"
    Application.DisplayAlerts = True

    'Here i create my email
    Dim Cdo_Message As New CDO.Message

    Set Cdo_Message.Configuration = GetSMTPGmailServerConfig()
    With Cdo_Message
        .To = "myadress@yahoo.fr"
        .From = Environ("username") + "@ErrorHandler.com"
        .Subject = "Test"
        .TextBody = "Hello"
        .send
    End With

    Set Cdo_Message = Nothing
End Sub

和我的gmail配置功能:

Function GetSMTPGmailServerConfig() As Object
Dim Cdo_Config As New CDO.Configuration
Dim Cdo_Fields As Object

Set Cdo_Fields = Cdo_Config.Fields
With Cdo_Fields
    .item(cdoSendUsingMethod) = cdoSendUsingPort
    .item(cdoSMTPServer) = "smtp.gmail.com"
    .item(cdoSMTPServerPort) = 465
    .item(cdoSendUserName) = "AIErrorHandler@gmail.com"
    .item(cdoSendPassword) = "mypassword"
    .item(cdoSMTPAuthenticate) = cdoBasic
    .item(cdoSMTPUseSSL) = True
    .Update
End With

但是在.send指令中,我遇到此错误:  sendusing configuration value is invalid

我尝试了3种可能的sendusing配置,但均未成功。

我正在寻找一种解决此问题的方法,或者在不使用Outlook的情况下通过vba发送电子邮件

0 个答案:

没有答案