从asp.net的gmail帐户中读取邮件

时间:2011-09-08 22:20:46

标签: asp.net pop3 gmail-pop incoming-mail

我想从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);
        }

任何人都可以告诉我如何从收件箱中获取实际邮件。

2 个答案:

答案 0 :(得分:1)

您是否看过Mail.net,我自己没有必要自己使用它,但我在这里做了很多好事。

干杯

答案 1 :(得分:0)

  1. 您应该在发出命令后立即阅读服务器响应。
  2. 您尚未成功登录Gmail - 您应该使用POP3命令,例如 USER PASS
  3. 如果电子邮件包含“-ERR”文本
  4. ,您的代码将失败
  5. str + = strTemp;当您使用大型附件恢复电子邮件时,将会杀死您的应用
  6. 我建议不要重新发明轮子 - 有POP3访问的库。

相关问题