MKR1010 using EMailSender library works twice then will not connect to email server

So I think I got to the bottom of this, or at least found a reasonable workaround

It's an omission in EMailSender library (at least for versions <= 2.4.1). The EMailSender::send never closes the WiFiClient/WiFiSSLClient causing a pile-up of open sockets to the smtp server from the same client. Why 2 is the limited? I don't know yet, but each EMailSender::send call opens a new socket to the server and leaves it open/in a "consumed" state until the WiFi is disconnected/reset. By exposing the WiFiClient _sock variable, you can see that with each email you sent, the ServerDrv::getSocket() returns a brand new socket incrementally with each email; when it gets to 2, starting the client fails. Interestingly enough, the WiFiClient::connect/connected functions can only go to 254 (255 indicates NO_SOCKET_AVAILABLE) so even if you can somehow get past _sock=1 successfully with the gmail smtp server, any code would only be able to send 255 emails and then it will stop working.

When editing the EMailSender library to call WiFiClient::stop() at the end of EMailSender::send, the _sock values are released (no more incrementing to 255) and I no longer get blocked on _sock=2 and emails send OK.

a small code to reproduce:

#include <EMailSender.h>
#include <WiFiNINA.h>

//please enter your sensitive data in the Secret tab
char ssid[] = "my_wifi_ssid";                // your network SSID (name)
char pass[] = "my_wifi_pwd";                // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;             // the Wi-Fi radio's status
int EMAIL_NUMBER = 0;

const char *email_recp = "my_to_email@gmail.com";

EMailSender emailSend("my_from_email@gmail.com", "my_from_email_pwd");

void sendEmail(String subjectStr, String messageStr) {
  
  EMailSender::EMailMessage message;
  message.subject = subjectStr;
  message.message = messageStr;
  bool statusB = false;
  while (!statusB) {
    EMailSender::Response resp = emailSend.send(email_recp, message);

    statusB = resp.status;

    Serial.println("Sending status: ");
    Serial.println(resp.status);
    Serial.println(resp.code);
    Serial.println(resp.desc);
  }
}

void setup() {  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial);

  // attempt to connect to Wi-Fi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to network: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  Serial.println("---------------------------------------");
  Serial.println("Attempting to send test email to my_to_email@gmail.com");
  sendEmail("From your friendly Arduino board", "The Nano 33 IoT has successfully started and connected to the internet.");
}

void loop() {
  String mess = "Attempt #" + String(++EMAIL_NUMBER);
  sendEmail("What's in a name", mess);
  delay (5000);
}

at the end of EMailSender::send in EMailSender.cpp:

  response.status = true;
  response.code = F("0");
  response.desc = F("Message sent!");

  return response;
}

result:
06:57:24.952 -> Attempting to connect to network: my_wifi_ssid
06:57:37.357 -> Attempting to connect to network: my_wifi_ssid
06:57:48.060 -> You're connected to the network
06:57:48.060 -> ---------------------------------------
06:57:48.060 -> Attempting to send test email to my_to_email@gmail.com
06:57:53.619 -> Sending status:
06:57:53.619 -> 1
06:57:53.619 -> 0
06:57:53.619 -> Message sent!
06:57:58.034 -> Sending status:
06:57:58.034 -> 1
06:57:58.034 -> 0
06:57:58.034 -> Message sent!
06:58:21.557 -> Sending status:
06:58:21.557 -> 0
06:58:21.557 -> 2
06:58:21.557 -> Could not connect to mail server

force WiFiClient/WiFiSSLClient to close in EMailSender::send

  response.status = true;
  response.code = F("0");
  response.desc = F("Message sent!");
  
  client.stop();

  return response;
}

result:
07:01:48.035 -> Attempting to connect to network: my_wifi_ssid
07:02:02.241 -> You're connected to the network
07:02:02.241 -> ---------------------------------------
07:02:02.241 -> Attempting to send test email to my_to_email@gmail.com
07:02:06.661 -> Sending status:
07:02:06.661 -> 1
07:02:06.661 -> 0
07:02:06.661 -> Message sent!
07:02:18.763 -> Sending status:
07:02:18.763 -> 1
07:02:18.763 -> 0
07:02:18.763 -> Message sent!
07:02:28.365 -> Sending status:
07:02:28.365 -> 1
07:02:28.365 -> 0
07:02:28.365 -> Message sent!
07:02:38.185 -> Sending status:
07:02:38.185 -> 1
07:02:38.185 -> 0
07:02:38.185 -> Message sent!
07:02:47.603 -> Sending status:
07:02:47.603 -> 1
07:02:47.603 -> 0
07:02:47.603 -> Message sent!
....and on and on and on