使用 VBA 通过 gmail 发送电子邮件的 CDO.message 的任何替代方案

问题描述 投票:0回答:1

当 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"" <[email protected]>"
    .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 应用程序.计算 = xlCalculationAutomatic

vba gmail-api
1个回答
0
投票

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

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

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]);
    }
  }
}

使用的资源:Gmail AppSpreadsheetApp

© www.soinside.com 2019 - 2024. All rights reserved.