.NET 4.x Прием электронных сообщений, POP3, SSL/TLS - C#
Формулировка задачи:
Добрый день, подскажите пожалуйста как реализовать прием писем с почты POP3 + SSL или TLS?
Просто изменить порт на 995 не катит
using System; using System.Text; using System.Net.Sockets; using System.Windows.Forms; namespace Pop3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { TcpClient tcpClient = new TcpClient(); tcpClient.Connect("pop.mail.ru", 110); NetworkStream netStrm = tcpClient.GetStream(); System.IO.StreamReader strRead = new System.IO.StreamReader(netStrm); if (tcpClient.Connected) { MessageBox.Show("connected: " + strRead.ReadLine()); } string login = "USER\r\n"; byte[] WriteBuffer = new byte[1024]; ASCIIEncoding en = new System.Text.ASCIIEncoding(); WriteBuffer = en.GetBytes(login); netStrm.Write(WriteBuffer, 0, WriteBuffer.Length); MessageBox.Show("response: " + strRead.ReadLine()); login = "PASSWORD\r\n"; WriteBuffer = en.GetBytes(login); netStrm.Write(WriteBuffer, 0, WriteBuffer.Length); MessageBox.Show("response: " + strRead.ReadLine()); login = "LIST\r\n"; WriteBuffer = en.GetBytes(login); netStrm.Write(WriteBuffer, 0, WriteBuffer.Length); string resp; while (true) { resp = strRead.ReadLine(); MessageBox.Show("could resp be .\r\n " + resp); if (resp == ".") { MessageBox.Show("yes"); break; } else { MessageBox.Show("list: " + resp); continue; } } login = "QUIT\r\n"; WriteBuffer = en.GetBytes(login); netStrm.Write(WriteBuffer, 0, WriteBuffer.Length); MessageBox.Show("response: " + strRead.ReadLine()); } private void btnConnect_Click(object sender, EventArgs e) { TcpClient tcpClient = new TcpClient(); txtLog.Text = "I say:\r\nConnect me to " + txtServer.Text + ":" + txtPort.Text + "\r\n\r\n"; tcpClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text)); NetworkStream netStream = tcpClient.GetStream(); System.IO.StreamReader strReader = new System.IO.StreamReader(netStream); if (tcpClient.Connected) { txtLog.Text += "Server says:\r\n" + strReader.ReadLine() + "\r\n\r\n"; byte[] WriteBuffer = new byte[1024]; ASCIIEncoding enc = new System.Text.ASCIIEncoding(); WriteBuffer = enc.GetBytes("USER " + txtUser.Text + "\r\n"); txtLog.Text += "I say:\r\nHere's the username: " + txtUser.Text + "\r\n\r\n"; netStream.Write(WriteBuffer, 0, WriteBuffer.Length); txtLog.Text += "Server says\r\n" + strReader.ReadLine() + "\r\n\r\n"; WriteBuffer = enc.GetBytes("PASS " + txtPass.Text + "\r\n"); txtLog.Text += "I say:\r\nHere's the password: " + txtPass.Text + "\r\n\r\n"; netStream.Write(WriteBuffer, 0, WriteBuffer.Length); txtLog.Text += "Server says:\r\n" + strReader.ReadLine() + "\r\n\r\n"; WriteBuffer = enc.GetBytes("LIST\r\n"); txtLog.Text += "I say:\r\nPlease list the messages\r\n\r\n"; netStream.Write(WriteBuffer, 0, WriteBuffer.Length); string ListMessage; while (true) { try { ListMessage = strReader.ReadLine(); if (ListMessage == ".") { break; } else { txtLog.Text += "Server says:\r\n" + ListMessage + "\r\n\r\n"; continue; } } catch (System.IO.IOException ex) { MessageBox.Show(ex.Message); } } txtLog.Text += "I say:\r\nThanks, we will disconnect now\r\n\r\n"; WriteBuffer = enc.GetBytes("QUIT\r\n"); netStream.Write(WriteBuffer, 0, WriteBuffer.Length); txtLog.Text += "Server says:\r\n" + strReader.ReadLine(); } } } }
Решение задачи: «.NET 4.x Прием электронных сообщений, POP3, SSL/TLS»
textual
Листинг программы
string PopHost; string PortNm; string UserName; string Password; string Message; string ServerCommand; byte[] m_buffer; string ListMsg; TcpClient clientConnect = new TcpClient(); NetworkStream _networkStream = default(NetworkStream); SslStream _sslStream = default(SslStream); StreamReader _StreamReader = default(StreamReader); private void btnConnect_Click(object sender, EventArgs e) { PopHost = txtHost.Text; PortNm = txtPort.Text; UserName = txtUsrNm.Text; Password = txtPass.Text; Message = MsgCount.Text; clientConnect = new TcpClient(PopHost, int.Parse(txtPort.Text)); _networkStream = clientConnect.GetStream(); _sslStream = new SslStream(_networkStream); _sslStream.AuthenticateAsClient(PopHost); _StreamReader = new StreamReader(_sslStream); ServerCommand = "USER " + UserName + "\r\n"; m_buffer = Encoding.ASCII.GetBytes(ServerCommand.ToCharArray()); _sslStream.Write(m_buffer, 0, m_buffer.Length); listBox1.Items.Add(_StreamReader.ReadLine()); ServerCommand = "PASS " + Password + "\r\n"; m_buffer = Encoding.ASCII.GetBytes(ServerCommand.ToCharArray()); _sslStream.Write(m_buffer, 0, m_buffer.Length); listBox1.Items.Add(_StreamReader.ReadLine()); ServerCommand = "STAT " + "\r\n"; m_buffer = Encoding.ASCII.GetBytes(ServerCommand.ToCharArray()); _sslStream.Write(m_buffer, 0, m_buffer.Length); listBox1.Items.Add(_StreamReader.ReadLine()); // +OK Welcome! ServerCommand = "LIST " + "\r\n"; m_buffer = Encoding.ASCII.GetBytes(ServerCommand.ToCharArray()); _sslStream.Write(m_buffer, 0, m_buffer.Length); listBox1.Items.Add(_StreamReader.ReadLine()); // кол-во сообщений и общий размер }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д