C# (dotnetcore) OpenPop.NETでGmailに接続してCAPAコマンドを実行
CAPAコマンドとは、POP3サーバーに実装されている機能を確認する為のコマンドです。
過去記事を振り返りしていたら、
以下の記事を見つけ、
C#でもUIDLが利用できるのか確認してみたので、記事にまとめときます。
ログ付きで確認
using System; using System.Collections.Generic; using OpenPop.Mime; using OpenPop.Pop3; namespace mail_console_example { public class Logger : OpenPop.Common.Logging.ILog { public void LogError(string message) { Console.WriteLine("LogError: " + message); } public void LogDebug(string message) { Console.WriteLine("LogDebug: " + message); } } class Program { static void Main(string[] args) { OpenPop.Common.Logging.DefaultLogger.SetLog(new Logger()); //Pop3Clientを生成 using (Pop3Client client = new Pop3Client()) { //pop3サーバーへの接続先 client.Connect("pop.gmail.com", 995, true); //認証 client.Authenticate("recent:egmailのメールアドレスを指定", "パスワードを設定",AuthenticationMethod.UsernameAndPassword); //CAPAコマンド実行 var capaResult = client.Capabilities(); //実行結果をConsoleに出力 foreach (var dic in capaResult) { Console.WriteLine("key:" + dic.Key); foreach (var item in dic.Value) { Console.WriteLine("val:" + item); } } } } } }
結果
~~~$ dotnet mail_console_example.dll LogDebug: Connect-Response: "+OK Gpop ready for requests from 2400:2650:740:3300:1898:424f:e2c0:a789 g11mb130412760jaj" LogDebug: SendCommand: "USER recent:~~省略~~@gmail.com" LogDebug: Server-Response: "+OK send PASS" LogDebug: SendCommand: "PASS ~~省略~~" LogDebug: Server-Response: "+OK Welcome." LogDebug: SendCommand: "CAPA" LogDebug: Server-Response: "+OK Capability list follows" key:USER key:RESP-CODES key:PIPELINING key:LOGIN-DELAY val:300 key:TOP key:UIDL key:X-GOOGLE-RICO
UIDLに関するコマンドの確認
using System; using System.Collections.Generic; using OpenPop.Pop3; namespace mail_console_example { public class Logger : OpenPop.Common.Logging.ILog { public void LogError(string message) { Console.WriteLine("LogError: " + message); } public void LogDebug(string message) { Console.WriteLine("LogDebug: " + message); } } class Program { static void Main(string[] args) { OpenPop.Common.Logging.DefaultLogger.SetLog(new Logger()); //Pop3Clientを生成 using (Pop3Client client = new Pop3Client()) { //pop3サーバーへの接続先 client.Connect("pop.gmail.com", 995, true); //認証 client.Authenticate("recent:egmailのメールアドレスを指定", "パスワードを設定",AuthenticationMethod.UsernameAndPassword); //メッセージ数取得 List<string> uids = client.GetMessageUids(); //メッセージ数の数だけ繰り返す for (int i = 0; i < uids.Count; i++) { Console.WriteLine("▽ ---- ▽"); //GetMessageUidsで取得した、UIDLの値 Console.WriteLine(uids[i]); //GetMessageUidで取得した、UIDLの値 Console.WriteLine(client.GetMessageUid(i + 1)); Console.WriteLine("△ ---- △"); } } } } }
結果
~~~$ dotnet mail_console_example.dll LogDebug: Connect-Response: "+OK Gpop ready for requests from 2400:2650:740:3300:1898:424f:e2c0:a789 i5mb138207653ivq" LogDebug: SendCommand: "USER recent:~~省略~~@gmail.com" LogDebug: Server-Response: "+OK send PASS" LogDebug: SendCommand: "PASS ~~省略~~" LogDebug: Server-Response: "+OK Welcome." LogDebug: SendCommand: "UIDL" LogDebug: Server-Response: "+OK" ▽ ---- ▽ GmailId174020bc97f1e54f LogDebug: SendCommand: "UIDL 1" LogDebug: Server-Response: "+OK 1 GmailId174020bc97f1e54f" GmailId174020bc97f1e54f △ ---- △ ▽ ---- ▽ GmailId174024c81ea33748 LogDebug: SendCommand: "UIDL 2" LogDebug: Server-Response: "+OK 2 GmailId174024c81ea33748" GmailId174024c81ea33748 △ ---- △ ▽ ---- ▽ GmailId17448d120a4ec5dc LogDebug: SendCommand: "UIDL 3" LogDebug: Server-Response: "+OK 3 GmailId17448d120a4ec5dc" GmailId17448d120a4ec5dc △ ---- △ LogDebug: SendCommand: "QUIT" LogDebug: Server-Response: "+OK Farewell."
以上。