Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Friends,

For the first time iam posting my problem,
Iam trying to read Email Programmatically using IMAP setting its is console

using System.Collections.Generic;
using System.Text;

namespace EmailConsole
    class Program
        static System.IO.StreamWriter sw = null;
        static System.Net.Sockets.TcpClient tcpc = null;
        static System.Net.Security.SslStream ssl = null;
        static string username, password;
        static string path;
        static int bytes = -1;
        static byte[] buffer;
        static StringBuilder sb = new StringBuilder();
        static byte[] dummy;
        static void Main(string[] args)
                //path = Environment.CurrentDirectory + "\\emailresponse.txt";
                path = @"D:\emailresponse.txt";
                if (System.IO.File.Exists(path))

                sw = new System.IO.StreamWriter(System.IO.File.Create(path));
                tcpc = new System.Net.Sockets.TcpClient("", );

                ssl = new System.Net.Security.SslStream(tcpc.GetStream());

                Console.WriteLine("username : ");
                username = Console.ReadLine();

                Console.WriteLine("password : ");
                password = Console.ReadLine();
                receiveResponse("$ LOGIN " + username + " " + password + "  \r\n");

                receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");

                receiveResponse("$ SELECT INBOX\r\n");

                receiveResponse("$ STATUS INBOX (MESSAGES)\r\n");

                Console.WriteLine("enter the email number to fetch :");
                int number = int.Parse(Console.ReadLine());

                receiveResponse("$ FETCH " + number + " body[header]\r\n");
                receiveResponse("$ FETCH " + number + " body[text]\r\n");

                receiveResponse("$ LOGOUT\r\n");
            catch (Exception ex)
                Console.WriteLine("error: " + ex.Message);
                if (sw != null)
                if (ssl != null)
                if (tcpc != null)
        static void receiveResponse(string command)
                if (command != "")
                    if (tcpc.Connected)
                        dummy = Encoding.ASCII.GetBytes(command);
                        ssl.Write(dummy, 0, dummy.Length);
                        throw new ApplicationException("TCP CONNECTION DISCONNECTED");

                buffer = new byte[2048];
                bytes = ssl.Read(buffer, 0, 2048);
                //byte[] buffer = new byte[client.ReceiveBufferSize];

                sb = new StringBuilder();

            catch (Exception ex)
                throw new ApplicationException(ex.Message);

But iam getting repeatedly following error :
Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.

What I have tried:

when I searched I found following ideas,
That's most likely due to some underlying proxy system closing your connection.
something on your machine is blocking your connection. Try running it with firewall/antivirus disabled. This is not permanent, just for testing if this is setup issue or code issue. If it passes without the firewall/antivirus then you have to find a way to allow it through them - add it to the firewall exceptons, etc.
problem solved when disabled the antivirus
I have tried with these ideas but still the error was not fixed. Kindly Help me.
Thanks in advance.
Updated 22-Mar-16 3:45am
Jochen Arndt 22-Mar-16 8:42am    
I have no idea what happens but it might help others if you can tell where the execption occurs (which steps have been passed).

Not related to the problem but what is the intention of these lines in receiveResponse()?

buffer = new byte[2048];
sb = new StringBuilder();

The buffers should be allocated only once and not with each function call.
FARONO 22-Mar-16 9:08am    
is your TcpClient ("", ) empty on purpose? Or do you mean to connect to "localhost"? (not very much experience with TcpClient, but the ones I used were never empty)
Jochen Arndt 22-Mar-16 9:22am    
It was TcpClient("", 993) when I edited the question to format the code but has been removed with the last edit by the questioner. I don't know why he removed that part.
FARONO 22-Mar-16 9:28am    
did you debug the code? where did it stop?
Jochen Arndt 22-Mar-16 9:30am    
I'm not the questioner!
I have only edited the question to make the code readable.

Share this answer
Rather than re-inventing the wheel, use an existing library - for example:
GitHub - jstedfast/MailKit: A cross-platform .NET library for IMAP, POP3, and SMTP.[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900