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

发布于 12-03 16:15 字数 2061 浏览 3 评论 0原文

我想从 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("[email protected]"); // 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);
        }

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

i want to read the mails from gmail account.. i tried to login.. and was successfull.. but unable to get the mails from the inbox..
below is the code i used for logging in...

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("[email protected]"); // 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);
        }

can anyone please tell me how can i get the actual mails from the inbox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖2024-12-10 16:15:06

你看过Mail.net吗,我自己不需要使用它,但我在这里关于它的好事情。

干杯

Have you had a look at Mail.net, I have not had need to use it myself but I here good things about it.

Cheers

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

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

  1. You should read server responses as soon as you issue a command.
  2. You have not logged in to Gmail successfully - you should use POP3 commands like USER, PASS
  3. Your code fails if email contains "-ERR" text
  4. str += strTemp; is going to kill your app when you revive email with a large attachment

I'd recommend not reinventing the wheel - there are libraries for POP3 access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文