Click here to Skip to main content
15,909,539 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

C#
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)
        {
            try
            {
                //path = Environment.CurrentDirectory + "\\emailresponse.txt";
                path = @"D:\emailresponse.txt";
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(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());
                ssl.AuthenticateAsClient("outlook.office365.com");
                receiveResponse("");

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

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

                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);
            }
            finally
            {
                if (sw != null)
                {
                    //sw.Close();
                    //sw.Dispose();
                }
                if (ssl != null)
                {
                    //ssl.Close();
                    //ssl.Dispose();
                }
                if (tcpc != null)
                {
                    //tcpc.Close();
                }
            }
            Console.ReadKey();
        }
        static void receiveResponse(string command)
        {
            try
            {
                if (command != "")
                {
                    if (tcpc.Connected)
                    {
                        dummy = Encoding.ASCII.GetBytes(command);
                        ssl.Write(dummy, 0, dummy.Length);
                    }
                    else
                    {
                        throw new ApplicationException("TCP CONNECTION DISCONNECTED");
                    }
                }
                ssl.Flush();


                buffer = new byte[2048];
                bytes = ssl.Read(buffer, 0, 2048);
                //byte[] buffer = new byte[client.ReceiveBufferSize];
                sb.Append(Encoding.ASCII.GetString(buffer));


                Console.WriteLine(sb.ToString());
                sw.WriteLine(sb.ToString());
                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.
Posted
Updated 22-Mar-16 3:45am
v5
Comments
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("outlook.office365.com", 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