EmailSender Help - nano 33 iot

I can't seem to get my arduino to send a basic email. Code is:

// #include "Arduino.h"
#include <EMailSender.h>
#include <WiFiNINA.h>

#include "arduino_secrets.h" 


///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
char emailpass[] = EMAIL_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status

EMailSender emailSend("(arduino-email)@gmail.com", emailpass);

//just for testing purposes
int distance = 53; 

void printWifiData() {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  printMacAddress(mac);
}

void connectWifi() {
  status = WiFi.status();
  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    if (status == WL_DISCONNECTED) {
      WiFi.end();
      Serial.println("Disconnected");
      delay(10000);
    }
    else if (status != WL_DISCONNECTED && status != WL_CONNECTED && status != WL_IDLE_STATUS) {
      WiFi.end();
      Serial.print("Not connected, not disconnected...Status: ");
      Serial.println(status);
      delay(10000);
    }
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
    
    // wait 10 seconds for connection:
    delay(10000);
  }
  status = WiFi.status();
  Serial.print("WiFi Status: ");
  Serial.println(status);
  printWifiData();
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  printMacAddress(bssid);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}

void sendEmail(String subjectStr, String messageStr) {
  EMailSender::EMailMessage message;
  message.subject = subjectStr;
  message.message = messageStr;
  
  status = WiFi.status();
  if (status != WL_CONNECTED) {
    connectWifi();
  }
  
  EMailSender::Response resp = emailSend.send("MY-EMAIL", message);
  delay(20000);
  
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);
}

void setup() {
  Serial.begin(38400);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(100); // Clears the trigPin
    
    if (distance >= 50) {      
      sendEmail("Purification System - Add Salt", "It is time to add more salt to the purification system.");
    }
    else {
      String part1 = "The System is working. The current level of salt is: "; 
      String part2 = part1 + distance;
      String message2 = part2 + " cm. System alert at 50 cm.";
      sendEmail("Purification System - Working", message2);
    }
  delay(60000);
}

Output from that is:

WiFi Status: 3
IP Address: (blanked out), internal IP on my network
MAC address: -------------- (blanked out)
Sending status: 
0
2
Could not connect to mail server
Disconnected
Attempting to connect to WPA SSID: (MY-SSID)
WiFi Status: 3
IP Address: (blanked out), internal IP on my network
MAC address: -------------- (blanked out)
Sending status: 
0
2
Could not connect to mail server

Before you ask, I know it connects fine to the WiFi. I can see it connect on my router. It is in Power Save mode and disconnects after about a minute. Arduino side, Rx from router 27Mb/s, Tx to router 1Mb/s. I know the Wifi SSID and password are correct as are the Gmail email and password.

Any ideas would be great?

I have a program on a Pi that sends email using gmail to warn me about freezing temperatures so I know when it is safe to put some plants outside.

Google requires that I configure my email account to be less secure to permit me to use it in this way, maybe you have a similar issue. Note too, that if I don't send mail for a while (i.e. over the summer) Google decides that I didn't really mean it and turns more secure settings back on.

I have Allow Less Secure Apps turned on. I created an email just for the arduino to use.

I'd be inclined to take a backup of the library and then start adding serial.prints to it.

That is not a bad idea. I am currently working backwords from an example from the library. Seeing if it is something in my own code first.

The other things I would check are whether it's https and what the WiFi packets look like using something like Wireshark.

Code "2" seems to be from this bit in the library:

  if(!client.connect(this->smtp_server, this->smtp_port)) {
	  response.desc = F("Could not connect to mail server");
	  response.code = F("2");
	  response.status = false;

	  return response;
  }

I don't see where you are setting the SMPT server and port.

I see that the default SMTP server built into the library is "smtp.gmail.com" and the port is 464 (SSL).

Did you create an Application Specific Password for your Gmail account?

https://support.google.com/mail/answer/185833

Do I need one if that GMail account does not use 2-Step verification. Allow Less-secure access is turned on in GMail settings.

Do you have a better idea?

What I am getting at, is that using App passwords requires turning on 2-step verification (making the account more secure). The Allow Less secure access is the opposite.

I will try it anyway.

I went to the library developer's forum and asked there. He responded and said that I needed to upload the google.com:443 certificate to the Wifi module. I did and it works fine now.