3

これはコードです:

protected void Button9_Click(object sender, EventArgs e)
{
    try
    {
        // create an instance of TcpClient
        TcpClient tcpclient = new TcpClient();

        // HOST NAME POP SERVER and gmail uses port number 995 for POP
        tcpclient.Connect("pop.gmail.com", 995);

        // This is Secure Stream // opened the connection between client and POP Server
        System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());

        // authenticate as client  
        sslstream.AuthenticateAsClient("pop.gmail.com");

        //bool flag = sslstream.IsAuthenticated;   // check flag

        // Asssigned the writer to stream 
        System.IO.StreamWriter sw = new StreamWriter(sslstream);

        // Assigned reader to stream
        System.IO.StreamReader reader = new StreamReader(sslstream);

        // refer POP rfc command, there very few around 6-9 command
        sw.WriteLine("USER your_gmail_user_name@gmail.com");

        // sent to server
        sw.Flush(); sw.WriteLine("PASS your_gmail_password");

        sw.Flush();

        // RETR 1 will retrive your first email. it will read content of your first email
        sw.WriteLine("RETR 1");
        sw.Flush();

        // close the connection
        sw.WriteLine("Quit ");
        sw.Flush(); string str = string.Empty;

        string strTemp = string.Empty;
        while ((strTemp = reader.ReadLine()) != null)
        {
            // find the . character in line
            if (strTemp == ".")
            {
                break;
            }
            if (strTemp.IndexOf("-ERR") != -1)
            {
                break;
            }
            str += strTemp;
        }

        textbox1.text = str;
        textbox1.text += "<BR>" + "Congratulation.. ....!!! You read your first gmail email ";
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

メッセージ本文は、ランダムな文字のように見えるものの集まりです。ランダムな文字の集まりだけでなく、解析して変換する必要のあるコードもあることを私は知っています。「メッセージ本文」の内容を読むにはどうすればよいですか?

4

1 に答える 1

6

私はあなたの答えに直接返信しているわけではありませんが、電子メールを読むことは非常に複雑な作業であり、自分で実装するのではなく、外部ライブラリを使用することでこれをより良く、より速く達成できると思います。

多くの優れた実装があります。私は通常、正常に動作し、オープンソースであるOpenPop.NETを使用します。

https://sourceforge.net/projects/hpop/

それは本当に人気があるので、あなたはインターネット上で多くの例を見つけることができます。

http://hpop.sourceforge.net/examples.php

すべてのメールを簡単に受け取ることができます。

using(Pop3Client client = new Pop3Client())
{
    // Connect to the server
    client.Connect("pop.gmail.com", 995, true);

    // Authenticate ourselves towards the server
    client.Authenticate("username@gmail.com", "password", AuthenticationMethod.UsernameAndPassword);

    // Get the number of messages in the inbox
    int messageCount = client.GetMessageCount();

    // We want to download all messages
    List<Message> allMessages = new List<Message>(messageCount);

    // Messages are numbered in the interval: [1, messageCount]
    // Ergo: message numbers are 1-based.
    // Most servers give the latest message the highest number
    for (int i = messageCount; i > 0; i--)
    {
        allMessages.Add(client.GetMessage(i));
    }
}

あなたは完全な生のメッセージを得ることができます

var mailbody = ASCIIEncoding.ASCII.GetString(message.RawMessage);

または、utf8でエンコードされた電子メールの場合:

var encodedStringAsBytes = System.Convert.FromBase64String(message.RawMessage); 
var rawMessage =System.Text.Encoding.UTF8.GetString(encodedStringAsBytes);

代わりに、メール本文のみが必要な場合は、メール構造を掘り下げる必要があります。

http://hpop.sourceforge.net/documentation/OpenPop~OpenPop.Mime.MessagePart.html

簡単な作業ではないことはわかっていますが、前述したように、メールは複雑なオブジェクトです。

于 2012-10-29T12:22:54.167 に答える