首页 > 解决方案 > 通过 Excel 发送电子邮件

问题描述

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

我在这里尝试使用 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 发送电子邮件

标签: excelvbagmail

解决方案


推荐阅读