【问题标题】:Reading mails from gmail account in asp.net在 asp.net 中从 gmail 帐户读取邮件
【发布时间】:2011-09-08 22:20:45
【问题描述】:

我想阅读来自 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);
        }

谁能告诉我如何从收件箱中获取实际邮件。

【问题讨论】:

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


    【解决方案1】:

    你看过Mail.net吗,我自己不需要使用它,但我在这里有一些好处。

    干杯

    【讨论】:

      【解决方案2】:
      1. 您应该在发出命令后立即读取服务器响应。
      2. 您尚未成功登录 Gmail - 您应该使用 POP3 命令,例如 USERPASS
      3. 如果电子邮件包含“-ERR”文本,您的代码将失败
      4. str += strTemp;当您恢复带有大附件的电子邮件时,会杀死您的应用程序

      我建议不要重新发明轮子 - 有用于 POP3 访问的库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-14
        • 2013-11-17
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-15
        相关资源
        最近更新 更多