阅读邮件从gmail帐户在asp.net



我想从gmail帐户读取邮件。我试着登录…而且成功了……但是无法从收件箱中取出邮件…下面是我用于登录的代码…

try
        {
            TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient
            tcpclient.Connect("pop.gmail.com", 995); // HOST NAME POP SERVER and gmail uses port number 995 for POP 
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); // This is Secure Stream // opened the connection between client and POP Server
            sslstream.AuthenticateAsClient("pop.gmail.com"); // authenticate as client 
            //bool flag = sslstream.IsAuthenticated; // check flag 
            System.IO.StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream
            System.IO.StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream
            sw.WriteLine("username@gmail.com"); // refer POP rfc command, there very few around 6-9 command
            sw.Flush(); // sent to server
            sw.WriteLine("password");
            sw.Flush();
            sw.WriteLine("RETR 1"); // this will retrive your first email 
            sw.Flush();
            sw.WriteLine("Quit "); // close the connection
            sw.Flush();
            string str = string.Empty;
            string strTemp = string.Empty;
            while ((strTemp = reader.ReadLine()) != null)
            {
                if (strTemp == ".") // find the . character in line
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }
            Response.Write(str);
            Response.Write("" + "Congratulation.. ....!!! You read your first gmail email ");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

谁能告诉我怎样才能从收件箱中得到实际的邮件?

你看过Mail.net吗?我自己没有必要使用它,但是我发现它有很多优点。

欢呼

  1. 你应该在你发出一个命令后立即读取服务器的响应。
  2. 你还没有登录到Gmail成功-你应该使用POP3命令像USERPASS
  3. 如果电子邮件包含"-ERR"文本,代码将失败
  4. str += strTemp;当你重启带有大型附件的电子邮件时,会杀死你的应用程序

我不建议你重新发明轮子-有库为POP3访问。

相关内容

  • 没有找到相关文章

最新更新