Чтение почты из учетной записи 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("[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);
        }

может кто-нибудь, пожалуйста, скажите мне, как я могу получить фактические письма из почтового ящика.


person Abbas    schedule 08.09.2011    source источник


Ответы (2)


  1. Вы должны читать ответы сервера, как только вы вводите команду.
  2. Вы не вошли в Gmail успешно. Используйте команды POP3, такие как USER, PASS.
  3. Ваш код не работает, если электронное письмо содержит текст «-ERR»
  4. стр += стрТемп; собирается убить ваше приложение, когда вы оживите электронную почту с большим вложением

Я бы не рекомендовал изобретать велосипед - есть библиотеки для доступа по протоколу POP3.

person Pawel Lesnikowski    schedule 12.09.2011

Вы видели Mail.net, у меня не было необходимости использовать его самому, но я здесь хорошие вещи об этом.

Ваше здоровье

person Iain    schedule 08.09.2011