-3

when CDO.message (SMTP server) VBA code is run it checks if that gmail ID (from which we are sending email) is linked with the current system or not. If it is run on a new system where we never logged in with that gmail id then it gives sever failing error and email is not sent. So I want ask some other way with code (may be gmail api) which does not check for system's link with gmail ID. BELOW IS THE CODE THAT I AM USING

 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

  • The answer here might help: https://stackoverflow.com/questions/48020578/excel-vba-cdo-mail – HackSlash Jul 20 '20 at 16:39
  • Hi ! So for the sake of clarification, you basically want to be able to send an email from CDO VBA code using [Gmail API](https://developers.google.com/gmail/api) right? What do you exactly mean by ```Gmail Id```? – Mateo Randwolf Jul 21 '20 at 07:20
  • Gmail id means our email id on gmail like abcd@gmail.com. – Rahul Mittal Jul 27 '20 at 16:59
  • problem is that if I am using abcd@gmail.com in cdo code to send email to some one then it works good on my pc as I have logged in abcd@gmail.com in my chrome browser. But if same VBA run on some other pc at far location then it fails and an email comes to abcd@gmail.com saying that 'A signin attempt was blocked'. So google thinks that some one unauthorised is trying to login in my account (abcd@gmail.com) and blocks it. So I want a method which works on every pc at every location. Hope you understand. – Rahul Mittal Jul 27 '20 at 17:06
  • And as I searched other method to send emails is Gmail API but didnt find any code for excel VBA. – Rahul Mittal Jul 27 '20 at 18:26
  • **What do you actually want to send with this automated email?** An excel sheet? A specific email? With [Gmail API](https://developers.google.com/gmail/api) or the [SMTP service of Gmail](https://developers.google.com/gmail/imap/imap-smtp) you will not be experiencing that issue as you are authorising your piece of code to send emails by using [oAuth 2.0](https://developers.google.com/identity/protocols/oauth2). – Mateo Randwolf Jul 28 '20 at 07:09
  • Actually I recognise a PC with its hardware id. When that file is run a pc it takes hardware id of pc and matches it with ids put on a sheet on a google drive. if does not match then an email comes to me using cdo that file has been run on a new system including its hardware id (text). but often cdo fails and emails comes to me 'A signin attempt was blocked'. – Rahul Mittal Jul 28 '20 at 21:33
  • I have shared code which I am using. – Rahul Mittal Jul 28 '20 at 21:46
  • So for your case scenario of sending an email depending on certain parameters on a spreadsheet if you want to use Gmail API I would suggest you integrating it with [Google Sheets](https://www.google.com/sheets/about/). In this way you can integrate both tools and even use [Apps Script](https://developers.google.com/apps-script) that might be easier and quicker to implement for you. Let me know if you would be open to a solution using this integration, for example an Apps Script script that sends these emails depending on certain values on a sheet (without permission errors). – Mateo Randwolf Jul 29 '20 at 07:16
  • okay. share it. Thank you. – Rahul Mittal Jul 29 '20 at 14:57

1 Answers1

0

Apps Script allows you to get information from Spreadsheets and other Google Documents and be able to use it to then send emails using its Gmail services under certain conditions met on these Spreadsheets for instance.

The following example is a simplification of your scenario where if two numbers / values do not match then you send an email to notify that the system has been run with another device. Below is the code with self-explanatory comments and an image representing the Spreadsheet I am using for this example.

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

enter image description here

Resources used: Gmail App and SpreadsheetApp

halfer
  • 19,824
  • 17
  • 99
  • 186
Mateo Randwolf
  • 2,823
  • 1
  • 6
  • 17
  • Thanks a lot for this. but sorry as I am beginner I am familiar only with excel VBA. I dont know how to use the above code. When you asked I thought it would be VBA macro or function. Now if possible please share with me either a VBA code to create and send email using GMAIL API (using API key, I hope it will not have that issue) or a VBA code to post something to a google sheet on drive on a specific range using GOOGLE ID of sheet and API key (I will post new detected pc's hardware id to it instead of email.) . Thank you. – Rahul Mittal Jul 30 '20 at 21:00
  • Hi! I am sorry you don't know yet about Google Sheets and Google Apps Scripts. Unfortunately, Gmail API is not supported in VBA, you can check which programming languages are supported [here](https://developers.google.com/gmail/api/downloads). To the second question, I am sorry I don't work with VBA so I don't know either if that is possible or how to do it. However, you could [take your excel sheet and import it into Google Sheets](https://support.google.com/docs/answer/9331167?hl=en). Let me know if that fits your scenario. :D – Mateo Randwolf Jul 31 '20 at 07:14
  • 1
    No, its okay. I will google and learn Google App Scripts. Thanks for your valuable time. – Rahul Mittal Jul 31 '20 at 10:22