-1

我可以通过以下代码在winform中阅读来自gmail、yahoo和hotmail的新邮件TcpClient。但我想获取由特定人发送或包含特定主题行的邮件。我搜索了更多文章,但我发现任何工作正常。谁能告诉我该怎么做?

 public string hotmail(string username, string password)
    {
        string result = "";
        string str = string.Empty;
        string strTemp = string.Empty;
        try
        {
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect("pop3.live.com", 995);
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
            sslstream.AuthenticateAsClient("pop3.live.com");
            System.IO.StreamWriter sw = new StreamWriter(sslstream);
            System.IO.StreamReader reader = new StreamReader(sslstream);
            strTemp = reader.ReadLine();
            sw.WriteLine("USER" + " " + username);
            sw.Flush();
            strTemp = reader.ReadLine();
            sw.WriteLine("PASS" + " " + password);
            sw.Flush();
            strTemp = reader.ReadLine();
            string[] numbers = Regex.Split(strTemp, @"\D+");
            int a = 0;
            foreach (string value in numbers)
            {
                if (!string.IsNullOrEmpty(value))
                {

                    int i = int.Parse(value);
                    numbers[a] = i.ToString();
                    a++;
                }
            }
            sw.WriteLine("RETR" + " " + numbers[0]);
            sw.Flush();
            strTemp = reader.ReadLine();
            while ((strTemp = reader.ReadLine()) != null)
            {
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }
            sw.WriteLine("Quit ");
            sw.Flush();
            result = str;
            return result;
         }
         Catch ( Exception ex)
         {}
         return result;
      }

谢谢n提前..

4

2 回答 2

1

POP3 不允许过滤要检索的邮件。如果你想在下载前过滤消息,你应该使用 IMAP4 协议。

请参阅 pop3 的 RFC:http ://www.ietf.org/rfc/rfc1939.txt和 IMAP4:https ://www.rfc-editor.org/rfc/rfc3501

于 2013-09-18T04:56:59.883 回答
1

根据@tray 的建议,您必须检索消息并根据地址或主题行对其进行过滤,如下所示:

public void check()
    {
        string sub;
        string result,from;
        int i = 1;
        do
        {
            ImapClient ic = new ImapClient("imap.mail.yahoo.com", "user@yahoo.com", "password", ImapClient.AuthMethods.Login, 993, true);
            ic.SelectMailbox("INBOX");
            int n = ic.GetMessageCount();

            MailMessage mail = ic.GetMessage(n - i);
            ic.Dispose();
            sub = mail.Subject;
            from = mail.From.ToString();
            result = mail.Raw;

            i++;
        } while (sub != "subject" || from == "person@example.com");
        string mailmsg = result;
    }

根据您的消息,这需要时间。欢呼...

于 2013-09-18T05:45:07.710 回答