Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:08:48+00:00 2026-05-24T20:08:48+00:00

I was reading C# 4.0 in a Nutshell by Joseph and Ben Albahari and

  • 0

I was reading C# 4.0 in a Nutshell by Joseph and Ben Albahari and came across this code in Networking chapter which reads the mails using POP3. POP3 has a defined communication as we all know. When I use the code in the chapter it looks obvious that it should work but it doesn’t. This is the code:

private static string ReadLine(Stream stream)
        {
            List<byte> list = new List<byte>();
            while (true)
            {
                int b = stream.ReadByte();
                if (b == 10 || b < 0) break;

                if (b != 13) list.Add( (byte)b);
            }
            return Encoding.UTF8.GetString(list.ToArray());
        }

        private static void SendCommand(Stream stream, string line)
        {
            byte[] byteArr = Encoding.UTF8.GetBytes(line + "\r\n");
            stream.Write(byteArr, 0, byteArr.Length);
            string response = ReadLine(stream);
            if (!response.StartsWith("+OK"))
                throw new Exception("Pop exception: " + response);
        }

        static void Main(string[] args)
        {

            using (TcpClient client = new TcpClient("pop.gmail.com", 995))
            {

                using (NetworkStream stream = client.GetStream())
                {
                    ReadLine(stream);
                }
            }

The code is incomplete in the sense that it doesn’t download mails. I was just trying to see what is the 1st reponse that we get from Gmail. But unfortunately, the program just stucks at ReadByte in ReadLine method. I think I should get this line when I first connect to the gmail:

+OK Hello there.

But my program just hangs. As per this page:

http://mail.google.com/support/bin/answer.py?answer=13287

you have to connect on pop.gmail.com which is exactly what I have done. Can anybody tell me what is missing?

Note: Do not send me any 3rd party projects to do this sort of this. I know it is very easy using them. But I am just trying to see what happens under the hoods. It would be better for me if you find the bug present in my program itself.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:08:49+00:00Added an answer on May 24, 2026 at 8:08 pm

    Use SslStream instead of NetworkStream as gmail requires ssl

    TcpClient objTcpClient = new TcpClient();
    //Connecting to the pop3 server at the specified port 
    objTcpClient.Connect(pop3Server, pop3PortNumber);
    
    //ValidateCertificate is a delegate
    SslStream netStream = new SslStream(objTcpClient.GetStream(), false, ValidateCertificate); //Authenticates the client on the server
    netStream.AuthenticateAsClient(pop3Server);
    //Stream Reader to read response stream 
    StreamReader netStreamReader = new StreamReader(netStream, Encoding.UTF8, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading Algorithms in a Nutshell (O'Reilly) and came across this symbol in
While reading C# 3.0 in a Nutshell by Joseph and Ben Albahari , I
Reading this Skip List implementation I came across this code fragment: typedef struct nodeStructure{
i have came across the sentence in a nutshell while reading a technical book
I'm reading C# 4.0 in a Nutshell by the Albahari brothers and I came
While reading the Hashing topic of C# in a nutshell book,i came across the
I was reading C# 4 in a Nutshell and I've come to this piece
Reading the documentation I thought this code would write logs to a file in
I am currently reading Albahari's C# 3.0 in a Nutshell book and on page
I'm currently reading the book C# 4.0 in a Nutshell , which by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.