Exemplo n.º 1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            TcpClient SmtpServer = new TcpClient("smtp.viettel.com.vn", 465);
            SslStream SmtpStream = new SslStream(SmtpServer.GetStream());
            TcpClient Pop3Server = new TcpClient("pop3.viettel.com.vn", 995);
            SslStream Pop3Stream = new SslStream(Pop3Server.GetStream());

            user     = txtUser.Text;
            password = txtPassword.Text;
            string result;

            Pop3Stream.AuthenticateAsClient("pop3.viettel.com.vn");
            SSLStream.ReadMessage(Pop3Stream);
            SmtpStream.AuthenticateAsClient("smtp.viettel.com.vn");
            SSLStream.ReadMessage(SmtpStream);
            SSLStream.WriteMessage(SmtpStream, "EHLO viettel.com.vn\r\n");
            SSLStream.ReadMessage(SmtpStream);
            SSLStream.WriteMessage(SmtpStream, "AUTH LOGIN\r\n");
            SSLStream.ReadMessage(SmtpStream);
            if (user == "")
            {
                lblNotification.Text = "Bạn chưa nhập tài khoản!";
                txtUser.Focus();
            }
            else
            {
                if (password == "")
                {
                    lblNotification.Text = "Bạn chưa nhập mật khẩu!";
                    txtPassword.Focus();
                }
                else
                {
                    SSLStream.WriteMessage(SmtpStream, string.Format("{0}\r\n", Convert.ToBase64String(Encoding.ASCII.GetBytes(user))));
                    SSLStream.ReadMessage(SmtpStream);
                    SSLStream.WriteMessage(SmtpStream, string.Format("{0}\r\n", Convert.ToBase64String(Encoding.ASCII.GetBytes(password))));
                    result = SSLStream.ReadMessage(SmtpStream);
                    if (result.Substring(0, 3) != "235")
                    {
                        lblNotification.Text = "Tài khoản hoặc mật khẩu bạn nhập không đúng. Hãy nhập lại!";
                        txtPassword.Text     = "";
                        txtPassword.Focus();
                    }
                    else
                    {
                        SSLStream.WriteMessage(Pop3Stream, String.Format("USER {0}\r\n", user));
                        SSLStream.ReadMessage(Pop3Stream);
                        SSLStream.WriteMessage(Pop3Stream, String.Format("PASS {0}\r\n", password));
                        SSLStream.ReadMessage(Pop3Stream);
                        ////SmtpServer.Close();
                        //Pop3Server.Close();
                        FrmMain FormMain = new FrmMain(SmtpStream, Pop3Stream, user);
                        FormMain.Show();
                        this.Dispose();
                    }
                }
            }
        }
Exemplo n.º 2
0
        private string ReadAllData(SslStream sslStream)
        {
            string allData = string.Empty;
            string partData;

            do
            {
                partData = SSLStream.ReadMessage(sslStream);
                allData += partData;
            } while (!partData.Contains("\r\n.\r\n"));
            return(allData);
        }
Exemplo n.º 3
0
 private void UpdateInbox()
 {
     SSLStream.WriteMessage(Pop3Stream, "STAT\r\n");
     MailQuantity             = int.Parse(SSLStream.ReadMessage(Pop3Stream).Split(' ')[1]);
     lblMailQuantity.Text     = string.Format("{0} messages", MailQuantity);
     webBrowser1.DocumentText = string.Empty;
     for (int i = MailQuantity; i > MailQuantity - 100; i--)
     {
         SSLStream.WriteMessage(Pop3Stream, string.Format("RETR {0}\r\n", i));
         string       allData    = ReadAllData(Pop3Stream);
         ListViewItem itemHeader = new ListViewItem(ReadSender(allData));
         string[]     timeSplit  = ReadTime(allData).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
         itemHeader.SubItems.Add(timeSplit[1] + " " + timeSplit[2] + " " + timeSplit[4].Substring(0, 5));
         itemHeader.SubItems.Add(ConvertSubject(ReadSubject(allData)));
         lvwInbox.Items.Add(itemHeader);
         lvwInbox.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
     }
 }
Exemplo n.º 4
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            //try
            {
                SSLStream.WriteMessage(SmtpStream, string.Format("MAIL FROM:<{0}>\r\n", user));
                /*MessageBox.Show(*/
                SSLStream.ReadMessage(SmtpStream) /*)*/;
                if (txtTo.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập địa chỉ người nhận.");
                    txtTo.Focus();
                }
                else
                {
                    SSLStream.WriteMessage(SmtpStream, string.Format("RCPT TO:<{0}>\r\n", txtTo.Text));
                    SSLStream.ReadMessage(SmtpStream);
                    SSLStream.WriteMessage(SmtpStream, "DATA\r\n");
                    SSLStream.ReadMessage(SmtpStream);
                    SSLStream.WriteMessage(SmtpStream, string.Format("Subject: {0}\r\n", txtSubject.Text));
                    SSLStream.WriteMessage(SmtpStream, string.Format("From: {0}\r\n", user));
                    SSLStream.WriteMessage(SmtpStream, string.Format("To: {0}\r\n", txtTo.Text));
                    SSLStream.WriteMessage(SmtpStream, string.Format("CC: {0}\r\n", txtCc.Text));
                    SSLStream.WriteMessage(SmtpStream, txtContent.Text + "\r\n");
                    SSLStream.WriteMessage(SmtpStream, ".\r\n");
                    if (SSLStream.ReadMessage(SmtpStream).Substring(0, 3) == "250")
                    {
                        MessageBox.Show("Gửi thành công!");
                        txtCc.Text      = "";
                        txtContent.Text = "";
                        txtSubject.Text = "";
                        txtTo.Text      = "";
                    }
                }
            }
            //catch (Exception ex)
            {
                //MessageBox.Show("Gửi mail bị lỗi. Vui lòng thử lại!");

                //throw ex;
            }
        }