【问题标题】:Any alternative to CDO.message for sending emails through gmail using VBA使用 VBA 通过 gmail 发送电子邮件的 CDO.message 的任何替代方案
【发布时间】:2020-07-30 15:34:48
【问题描述】:

当 CDO.message(SMTP 服务器)VBA 代码运行时,它会检查该 gmail ID(我们从中发送电子邮件)是否与当前系统相关联。如果它在我们从未使用该 gmail id 登录的新系统上运行,那么它会给出服务器失败错误并且不会发送电子邮件。所以我想用其他方式询问代码(可能是gmail api),它不会检查系统与gmail ID的链接。 以下是我正在使用的代码

 Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Dim email As String
Dim pass As String
Dim CN As String
Dim OS As String
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1
Set Flds = iConf.Fields

With Flds

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = FF
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpaccountname") = "abcd"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = DD
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    .Update
    
End With

 With iMsg
    Set .Configuration = iConf
    .To = FF
    .CC = ""
    .BCC = ""
    .From = """from"" <Reply@something.nl>"
    .Subject = UN & " C1 LOGGED IN"
    .TextBody = "COMPUTER NAME IS -" & CPN & ", USERNAME NAME IS -" & UN & ", COMPUTER ID IS -" & sAns
    .Send
    
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic

【问题讨论】:

  • 这里的答案可能会有所帮助:stackoverflow.com/questions/48020578/excel-vba-cdo-mail
  • 嗨!因此,为了澄清起见,您基本上希望能够使用Gmail API 从 CDO VBA 代码发送电子邮件,对吧? Gmail Id 到底是什么意思?
  • Gmail id 表示我们在 gmail 上的电子邮件 ID,例如 abcd@gmail.com。
  • 问题是,如果我在 cdo 代码中使用 abcd@gmail.com 向某人发送电子邮件,那么它在我的电脑上运行良好,因为我已经在我的 chrome 浏览器中登录 abcd@gmail.com .但是,如果相同的 VBA 在远处的其他电脑上运行,那么它会失败,并且会向 abcd@gmail.com 发送一封电子邮件,说“登录尝试被阻止”。所以谷歌认为有人未经授权试图登录我的帐户(abcd@gmail.com)并阻止它。所以我想要一种适用于每个位置的每台电脑的方法。希望你能理解。
  • 当我搜索其他发送电子邮件的方法是 Gmail API 但没有找到任何用于 excel VBA 的代码。

标签: vba gmail-api


【解决方案1】:

解决方案

Apps Script 允许您从电子表格和其他 Google 文档中获取信息,并能够使用它然后在这些电子表格上满足的某些条件下使用其 Gmail 服务发送电子邮件。

以下示例是您的场景的简化,如果两个数字/值不匹配,则您发送电子邮件通知系统已与另一台设备一起运行。下面是带有不言自明的 cmets 的代码和代表我在此示例中使用的电子表格的图像。

function myFunction() {
  // Get the sheet we will be using
  var ss = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  // get the values of the range that contains the content
  // flat is used to get the 2D array returned by getValues() into a simple
  // 1D array with these values
  var content = ss.getRange('A1:A3').getValues().flat();
  // get the values of the range that contains the condition
  var condition1 = ss.getRange('B1:B3').getValues().flat();
  var condition2 = ss.getRange('C1:C3').getValues().flat();
  // get the values of the range that contains the email address
  var email = ss.getRange('D1:D3').getValues().flat();
  
  // iterate over all the values of the content column
  for(i=0;i<content.length;i++){
  // if the column B and C have different values in the row
    if(condition1[i]!=condition2[i]){
    // send emails with the appropiate properties
      GmailApp.sendEmail(email[i], 'Generated email', content[i]);
    }
  }
}

使用的资源:@​​987654323@ 和 SpreadsheetApp

我希望这对您有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。 :)

【讨论】:

  • 非常感谢。但对不起,因为我是初学者,我只熟悉 excel VBA。我不知道如何使用上面的代码。当您问我时,我认为这将是 VBA 宏或函数。现在,如果可能的话,请与我分享使用 GMAIL API 创建和发送电子邮件的 VBA 代码(使用 API 密钥,我希望它不会出现这个问题)或 VBA 代码以将某些内容发布到特定范围内驱动器上的谷歌表使用工作表的 GOOGLE ID 和 API 密钥(我会将新检测到的 pc 的硬件 ID 发布到它而不是电子邮件。)。谢谢。
  • 嗨!很抱歉,您还不了解 Google 表格和 Google Apps 脚本。不幸的是,VBA 不支持 Gmail API,您可以查看支持哪些编程语言here。对于第二个问题,我很抱歉我不使用 VBA,所以我不知道这是否可能或如何去做。但是,您可以take your excel sheet and import it into Google Sheets。让我知道这是否适合您的情况。 :D
  • 不,没关系。我会谷歌并学习谷歌应用脚​​本。感谢您宝贵的时间。
猜你喜欢
  • 1970-01-01
  • 2015-10-21
  • 2011-02-08
  • 2012-01-07
  • 2023-03-19
  • 2011-06-08
  • 2017-05-12
相关资源
最近更新 更多