Просмотр сообщений POP3 - C#
Формулировка задачи:
Доброго времени суток! мне нужно создать что-то подобное на почтовый клиент, отправку сообщений реализовать удалось без проблем с помощью smtp, а вот загрузить принятые сообщения никак не получается, знаю что реализовать это возможно с помощью pop3 протокола, кто знает - буду очень признателен!
Решение задачи: «Просмотр сообщений POP3»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
namespace Pop3Check
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("mail.geekpedia.com", 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 [email]support@geekpedia.com[/email]\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: " + strR[OFF]ead.ReadLine());
login = "PASS c0d3supp\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, WriteBu[/OFF]ffer.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;
}
}
// Disconnect from the POP3 server
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)
{
// Create a TCP client for a TCP connection
TcpClient tcpClient = new TcpClient();
txtLog.Text = "I say:\r\nConnect me to " + txtServer.Text + ":" + txtPort.Text + "\r\n\r\n";
// Connect this TCP client to the server IP/name and port specified in the form
tcpClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text));
// Create a network stream to retrieve data from the TCP client
NetworkStream netStream = tcpClient.GetStream();
// We need a stream reader to be able to read the network stream
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);
// If the connection was made successfully
if (tcpClient.Connected)
{
txtLog.Text += "Server says:\r\n" + strReader.ReadLine() + "\r\n\r\n";
// Buffer to which we're going to write the commands
byte[] WriteBuffer = new byte[1024];
// We're passing ASCII characters
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
// Pass the username to the server
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";
// Pass the password to the server
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";
// Now that we are (probably) authenticated, list the messages
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)
{
ListMessage = strReader.ReadLine();
if (ListMessage == ".")
{
// It's the last message so exit the loop and continue
break;
}
else
{
// List the message
txtLog.Text += "Server says:\r\n" + ListMessage + "\r\n\r\n";
continue;
}
}
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();
}
}
}
}