PopClient/C#

スペシャルサンクス:
.NETでPOPサーバからメールを受信する方法
C#でAPOP

ソース:

class PopClient : System.Net.Sockets.TcpClient
{
    public const int POP_PORT = 110;
    string ApopKey = "";
    private void SendLine(string s)
    {
        byte[] b = Encoding.ASCII.GetBytes(s + "\r\n");
        base.GetStream().Write(b, 0, b.Length);
    }
    private string ReadLine()
    {
        System.IO.StreamReader reader = new System.IO.StreamReader(base.GetStream(), System.Text.Encoding.ASCII);
        return reader.ReadLine();
    }
    public PopClient(string hostname, int port) : base (hostname, (port < 0) ? port : POP_PORT)
    {
        string s = this.ReadLine();
        if (!s.StartsWith("+OK"))
        {
            throw new PopClientException("接続時に POP サーバが \"" + s + "\" を返しました。");
        }
        // APOP可能かどうか判断
        System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"^.+(<.+>).*$");
        if(m.Success)
        {
            this.ApopKey = m.Groups[1].Value;
        }
    }
    private string ComputeHash(string s)
    {
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5CryptoServiceProvider.Create();
        byte[] bhash = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(s));
        string ret = "";
        foreach (byte b in bhash)
        {
            ret += string.Format("{0:X2}", b);
        }
        return ret;
    }
    public void Login(string username, string password)
    {
        if (!(string.IsNullOrEmpty(ApopKey)) && ApopKey.Length > 0)
        {
            this.SendLine("APOP " + username + " " + ComputeHash(this.ApopKey + password));
            string s = this.ReadLine();
            if (!s.StartsWith("+OK"))
            {
                throw new PopClientException("APOP 送信時に POP サーバが \"" + s + "\" を返しました。");
            }
        }
        else
        {
            // ユーザー名送信
            this.SendLine("USER " + username);
            string s = this.ReadLine();
            if (!s.StartsWith("+OK"))
            {
                throw new PopClientException("USER 送信時に POP サーバが \"" + s + "\" を返しました。");
            }
            // パスワード送信
            this.SendLine("PASS " + password);
            s = this.ReadLine();
            if (!s.StartsWith("+OK"))
            {
                throw new PopClientException("PASS 送信時に POP サーバが \"" + s + "\" を返しました。");
            }
        }
    }
    public new void Close()
    {
        this.SendLine("QUIT\r\n");
        this.ReadLine();
        base.Close();
    }
}

APOP動作に関しては未確認。
実際にメールを取得するには、LISTコマンド等の実装が必要。
間違いが含まれる場合が多々ありますので、流用には十分な検証の上で。

トラックバック(0)

トラックバックURL: http://blog.fne.jp/mt/mt-tb.cgi/1

コメントしちゃいなよ

サイトカウンタ

Total: 211,632 Hits, (Recent24Hours: 0 Hits)
~ Since 2010/01 ~

埋め込みツイッター

取得失敗;;
Now: 2024/03/19 13:58:26 JST

このアーカイブについて

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

月別 アーカイブ

2024年2月

 日   月   火   水   木   金   土 
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    

埋め込みQRコード