Login in email server and get email using CSharp TcpClient
 

The Logic (or protocol) behind the code is :- 

 Client   : +OK Server POP  Ready!!
 Client   : USER xxx
 Server   : +OK
 Client   : PASS yyy
 Server   : +OK user successfully logged on
 Client   : STAT
 Server   : +OK n m
 Client   : RETR 1
 Server   : +OK
               ---{ data }-----
 Client   : QUIT
 Server   : +OK Server POP signing off

 

Now the C# code is :-

 

//in this code POPServ.Text is the server address like imap.google.com OR imap.fusioninfosoft.in OR imap.server.xx

//in this code txtUSer.Text  is the ussername of mail server and txtPassw.Text  is password for that email account

// since this procedure is working in another thread, updateUI_aadListBox() will update UI from thread

try
                {
                    int port = 110;
                    // create server POP3 with port 110
                    Data = POPServ.Text + "@" + port;
                    updateUI_aadListBox("Client:  " + Data);
                    Server = new TcpClient(POPServ.Text, 110);
                    // initialization
                    NetStrm = Server.GetStream();
                    RdStrm = new StreamReader(Server.GetStream());

                    updateUI_aadListBox("" + RdStrm.ReadLine());

                    // Login Process
                    Data = "USER " + txtUSer.Text + CRLF;
                    updateUI_aadListBox("Client:  " + Data);
                    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                    NetStrm.Write(szData, 0, szData.Length);
                    updateUI_aadListBox("" + RdStrm.ReadLine());

                    Data = "PASS " + txtPassw.Text + CRLF;
                    updateUI_aadListBox("Client:  " + Data);
                    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                    NetStrm.Write(szData, 0, szData.Length);
                    updateUI_aadListBox("" + RdStrm.ReadLine());

                    // Send STAT command to get information ie: number of mail and size
                    Data = "STAT" + CRLF;
                    updateUI_aadListBox("Client:  " + Data);
                    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                    NetStrm.Write(szData, 0, szData.Length);
                    updateUI_aadListBox("number of mail and size:" + RdStrm.ReadLine());


                    // Retrive mail 
                    Data = "RETR 1" + CRLF;
                    updateUI_aadListBox("Client:  " + Data);
                    szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                    NetStrm.Write(szData, 0, szData.Length);
                    updateUI_aadListBox("" + RdStrm.ReadLine());
                    string email = "";
                    while (true)
                    {
                        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                        if (szData.Length <= 0)
                            break;
                        NetStrm.Write(szData, 0, szData.Length);
                        string line=RdStrm.ReadLine();
                        updateUI_aadListBox("" +line );
                        email = email + line;
                        ChitrakshMethods.AppendLineToFile("maildata.txt", line);
                    }
                    // change enabled - disabled button
                    if (btnGetMails.InvokeRequired)
                    {
                        btnGetMails.Invoke(new MethodInvoker(() =>
                        {
                            btnGetMails.Enabled = true;
                        }));
                    }

                }
                catch (SocketException sex)
                {
                    updateUI_aadListBox("Error: " + sex.Message);
                    MessageBox.Show(sex.ToString());
                }
                catch (InvalidOperationException err)
                {
                    updateUI_aadListBox("Error: " + err.Message);
                    MessageBox.Show(err.ToString());
                }

share on whatapp
447 Views

Comments

Show All Amazon Product

Private Policy   Terms of Service