Esempio n. 1
0
        static void Main(string[] args)
        {
            try
            {
                // there should be no gap between the imap command and the \r\n
                // ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server
                // cannot check for \r\n because in case of larger response from server ex:read email message
                // there are lot of lines so \r \n appears at the end of each line
                //ssl.timeout sets the underlying tcp connections timeout if the read or write
                //time out exceeds then the undelying connection is closed
                tcpc = new System.Net.Sockets.TcpClient("imap.mail.ru", 993);

                ssl = new System.Net.Security.SslStream(tcpc.GetStream());
                ssl.AuthenticateAsClient("imap.mail.ru");
                receiveResponse("");

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

                // Getting amount of Inbox
                String   inbox = receiveResponse("$ SELECT INBOX\r\n");
                string[] words = inbox.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                words[1] = words[1].Trim(new char[] { ' ', 'E' }); // " xx EXISTS"
                string subString        = "EXISTS";
                int    indexOfSubstring = words[1].IndexOf(subString);
                words[1] = words[1].Substring(0, indexOfSubstring - 1); // "xx"
                int amount = Convert.ToInt32(words[1]);
                Console.WriteLine("Всего писем: " + amount + "\r\n");
                for (int i = 0; i < amount; i++)
                {
                    Console.WriteLine("Письмо №{0}" + ":", i + 1);
                    GetMail(i);
                }
                //receiveResponse("$ LOGOUT\r\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine("error: " + ex.Message);
            }
            finally
            {
                if (ssl != null)
                {
                    ssl.Close();
                    ssl.Dispose();
                }
                if (tcpc != null)
                {
                    tcpc.Close();
                }
            }


            Console.ReadKey();
        }
Esempio n. 2
0
 public void Disconnect()
 {
     if (ssl != null)
     {
         ssl.Close();
         ssl.Dispose();
     }
     if (tcpc != null)
     {
         tcpc.Close();
     }
 }
 /// <summary>
 /// Saving a mail message before beeing sent by the SMTP client
 /// </summary>
 /// <param name="self">The caller</param>
 /// <param name="imapServer">The address of the IMAP server</param>
 /// <param name="imapPort">The port of the IMAP server</param>
 /// <param name="userName">The username to log on to the IMAP server</param>
 /// <param name="password">The password to log on to the IMAP server</param>
 /// <param name="sentFolderName">The name of the folder where the message will be saved</param>
 /// <param name="mailMessage">The message being saved</param>
 public static void SendAndSaveMessageToIMAP(this System.Net.Mail.SmtpClient self, System.Net.Mail.MailMessage mailMessage, string imapServer, int imapPort, string userName, string password, string sentFolderName)
 {
     try
     {
         path = System.Environment.CurrentDirectory + "\\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(imapServer, imapPort);
         ssl  = new System.Net.Security.SslStream(tcpc.GetStream());
         ssl.AuthenticateAsClient(imapServer);
         SendCommandAndReceiveResponse("");
         SendCommandAndReceiveResponse(string.Format("$ LOGIN {1} {2}  {0}", System.Environment.NewLine, userName, password));
         using (var m = mailMessage.RawMessage())
         {
             m.Position = 0;
             var sr    = new System.IO.StreamReader(m);
             var myStr = sr.ReadToEnd();
             SendCommandAndReceiveResponse(string.Format("$ APPEND {1} (\\Seen) {{{2}}}{0}", System.Environment.NewLine, sentFolderName, myStr.Length));
             SendCommandAndReceiveResponse(string.Format("{1}{0}", System.Environment.NewLine, myStr));
         }
         SendCommandAndReceiveResponse(string.Format("$ LOGOUT{0}", System.Environment.NewLine));
     }
     catch (System.Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("error: " + ex.Message);
     }
     finally
     {
         if (sw != null)
         {
             sw.Close();
             sw.Dispose();
         }
         if (ssl != null)
         {
             ssl.Close();
             ssl.Dispose();
         }
         if (tcpc != null)
         {
             tcpc.Close();
         }
     }
     self.Send(mailMessage);
 }
        public void DcFromGmail()         //---#2 log off
        {
            ReceiveResponse(prefix + " LOGOUT\r\n");

            if (answer.Contains("OK"))
            {
                userAvailable = false;
            }
            else
            {
                userAvailable = true;
            }

            if (ssl != null)
            {
                ssl.Close();
                ssl.Dispose();
            }
            if (tcpc != null)
            {
                tcpc.Close();
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            try
            {
                path = Environment.CurrentDirectory + "\\emailresponse.txt";

                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }

                sw = new System.IO.StreamWriter(System.IO.File.Create(path));
                // there should be no gap between the imap command and the \r\n
                // ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server
                // cannot check for \r\n because in case of larger response from server ex:read email message
                // there are lot of lines so \r \n appears at the end of each line
                //ssl.timeout sets the underlying tcp connections timeout if the read or write
                //time out exceeds then the undelying connection is closed
                tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);

                ssl = new System.Net.Security.SslStream(tcpc.GetStream());
                ssl.AuthenticateAsClient("imap.gmail.com");
                receiveResponse("");
                receiveResponse("$ CAPABILITY\r\n");
                Console.WriteLine("username : "******"password : "******"$ 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());

                fetchEmailHeader(number);
                fetchEmailBody(number);
                Console.WriteLine(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();
        }