Sending Emails when Alarm is Detected

My goal is to send email notifications when an alarm is detected via my sound sensor.
I have an ESP8622 ESP01 WiFi transceiver that I was able to connect to my network. I have the code for the Arduino/sound sensor (and it works) but I am having trouble with the code for the ESP8622.

Here is my Arduino Code:

const int soundPin = 2; // Digital pin connected to sound sensor
int soundState; 

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(soundPin, INPUT); // Set the sound sensor pin as input
}

void loop() {
  // Read the state of the sound sensor
  soundState = digitalRead(soundPin); 

  // Check if sound is detected (HIGH = sound detected)
  if (soundState == LOW) { 
    Serial.println("Alarm detected!"); 
    delay(1000); // Add delay to prevent rapid triggering
  }

  // Optional: Add a small delay to reduce serial monitor flooding
  delay(100); 
}

Here is the code for the ESP8622 (does not work):

#include <SMTPClient.h>

// Replace with your network credentials
const char* ssid = "****";
const char* password = "*****";

// Email credentials
const char* smtp_server = "smtp.gmail.com"; // e.g., smtp.gmail.com
const int smtp_port = 587; // common port for SMTP
const char* email_user = "****@gmail.com"; // Your email
const char* email_password = "******"; // Your email password
const char* recipient_email = "******"; // Recipient email

SMTPClient smtpClient;

void setup() {
  Serial.begin(9600);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  
  smtpClient.setServer(smtp_server, smtp_port);
  smtpClient.setLogin(email_user, email_password);
}

void loop() {
  // Check for incoming data from Arduino
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n');
    
    if (command.equals("SEND_EMAIL")) {
      sendEmail();
    }
  }
}

void sendEmail() {
  // Set email properties
  smtpClient.setSender(email_user);
  smtpClient.addRecipient(recipient_email);
  smtpClient.setSubject("Alarm Notification");
  smtpClient.setMessage("Alarm detected! An alert has been triggered.");

  // Send the email
  if (smtpClient.send()) {
    Serial.println("Email sent successfully");
  } else {
    Serial.print("Error sending email: ");
    Serial.println(smtpClient.getError());
  }

  // Clear the client for the next email
  smtpClient.clearAllRecipients();
}

Compilation error: 'class SMTPClient' has no member named 'setServer'; did you mean 'String SMTPClient::server'? (not accessible from this context)

Which library are you using? Is it this one?

I have ESP8266 SMTPClient library installed.

I guessed you did :-), but exactly which one is it?
Is it this one:


The reason I'm asking is so that I can have a look at the .h file for exactly the same library. There's no point if I happen to Google and find one that's different to the one you are using. If you post a screen shot of it, like the one above, then I'll know.
[Edit: It would also help if you said where you got the example code for sending mail]

I have found different parts of the code from multiple forums so I can't really provide one source for you. Thank you so much for your help with this.

Also the board that I have selected is the NodeMCU 1.0(ESP-12E) but I am not sure if that is correct.

I think you are using the first one in your screen shot. The second one's .h file name doesn't match the code you posted.
The first one is listed in the Arduino libraries here and the .h file name matches the one you are using.
There's an example in the library's zip file:

#include <ESP8266WiFi.h>
#include "SMTPClient.h"

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

SMTPClient smtp;

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(1000);

    smtp.begin("smtp.gmail.com", 465); // For SSL
    smtp.setCredentials("your_email@gmail.com", "your_password");

    if (smtp.sendMail("your_email@gmail.com", "recipient_email@gmail.com", "Test Subject", "This is a test email.")) {
        Serial.println("Email sent!");
    } else {
        Serial.println("Failed to send email.");
    }
}

void loop() {}

Have you seen that already? If I were you, I'd try that example, unmodified (apart from the Wifi credentials and the e-mail addresses), and see if it works.

If you have problems, it might be worth considering following this tutorial. Sara, one of the site owners, responds to question posted on that page. It's current, there are questions and answers from a couple of weeks ago, so it's still being supported