m5stackで組込み!!

Arduinoによるm5stack開発のいろいろと...

SSLメールでメール受信してみた

前回まではメール送信でしたが、今度はメール受信をしてみたいと思います。
ベースにするのはこちらのMailer.hを受信用に書き換えました。
www.kerislab.jp

サンプルコード(Mailer_pop.h)

#pragma once

#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <base64.h>

class Mailer_pop {
  public:
    Mailer_pop(const char* username, const char* password, const int pop_port, const char* pop_hostname):
      username(username), password(password), pop_port(pop_port), pop_hostname(pop_hostname) {}

    bool receive(void) {
      WiFiClientSecure client;
      String root_ca;
      
      /* ファイルオープン */
      File datFile = SD.open("/set/yahoo_root.cer");  /* 証明書を読みだす */
      if( datFile )
      {
        M5.Lcd.println("Root File open successful");
        Serial.println("Root File open successful");
        /* サイズ分ループ */
        while( datFile.available() )
        {
          root_ca = root_ca + datFile.readString();
        }
        /* ファイルクローズ */   
        datFile.close();
      } 
      else
      {
        M5.Lcd.println("Root File open error");
      }
      client.setCACert(root_ca.c_str()); /* 証明書の設定 */

      M5.Lcd.printf("Connecting to %s\n", pop_hostname);

      Serial.printf("Connecting to %s\n", pop_hostname);
      if (!client.connect(pop_hostname, pop_port)) {
        Serial.println("Could not connect to mail server");
        return false;
      }
      if (!readResponse(client, "+OK")) {
        Serial.println("Connection Error");
        return false;
      }
      client.println("USER " + String(username));
      if (!readResponse(client, "+OK")) {
        Serial.println("USER error");
        return false;
      }
      client.println("PASS " + String(password));
      if (!readResponse(client, "+OK")) {
        Serial.println("PASS error");
        return false;
      }
      client.println("STAT");
      if (!readResponse(client, "+OK")) {
        Serial.println("STAT failed");
        return false;
      }
//      client.println("LIST");
//      if (!readResponse(client, "+OK")) {
//        Serial.println("LIST failed1");
//        return false;
//      }
      client.println("RETR 1");
      if (!readResponse(client, "+OK")) {
        Serial.println("RETR failed2");
        return false;
      }
      client.println("QUIT");
      if (!readResponse(client, "+OK")) {
        Serial.println("QUIT failed");
        return false;
      }
      M5.Lcd.println("Received E-mail Successful");
      Serial.println("Received E-mail Successful");
      return true;
    }

  private:
    const char* username;
    const char* password;
    const int pop_port;
    const char* pop_hostname;

    bool readResponse(WiFiClientSecure &client, const String &target, uint32_t timeout_ms = 10000) {
      uint32_t timeStamp = millis();
      while (1) {
        if (client.available()) break;
        if (millis() > timeStamp + timeout_ms) {
          Serial.println("POP Response TIMEOUT!");
          return false;
        }
        delay(1);
      }
//      String res = client.readStringUntil('\n');
      String res = client.readString();
      res.trim();
      Serial.printf("Response: %s\n", res.c_str());
      if (target != "" && res.indexOf(target) == -1) return false;
      return true;
    }
};

POP形式で受信します。受信するメールはメールサーバで最古のものになります。
ここから削除したり、最新のメールを受信したりしていきたいと思います。