IMAP Почтовый клиент - C#
Формулировка задачи:
Уважаемые!
Нашёл пример IMAP клиента https://code.msdn.microsoft.com/wind...LIENT-b249d2e6
Столкнулся со следующей проблемой. Когда посылаю команду, например, LIST "" "*", возвращается только одна строка ответа. При дальнейшем считывании ssl потока, считываются и остальные. Вопрос в том, какое условие поставить для цикла чтения из ssl? Не парсить же, пока придёт ответ "OK LIST Completed".
using System;
using System.Text;
namespace _test_email_console
{
class Program
{
static System.Net.Sockets.TcpClient tcpc = null;
static System.Net.Security.SslStream ssl = null;
static string username, password;
static int bytes = -1;
static byte[] buffer;
static StringBuilder sb = new StringBuilder();
static byte[] dummy;
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.yandex.ru", 993);
ssl = new System.Net.Security.SslStream(tcpc.GetStream());
ssl.AuthenticateAsClient("imap.yandex.ru");
receiveResponse("");
Console.WriteLine("username : ");
username = Console.ReadLine();
Console.WriteLine("password : ");
password = Console.ReadLine();
receiveResponse("$ LOGIN " + username + " " + password + "\r\n");
receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");
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();
}
static void receiveResponse(string command)
{
try
{
Console.WriteLine("<<<" + command);
if (command != "")
{
if (tcpc.Connected)
{
dummy = Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0, dummy.Length);
}
else
{
throw new ApplicationException("TCP CONNECTION DISCONNECTED");
}
}
// Какое условие должно быть в этом цикле?
//while (bytes > 0)
//{
ssl.Flush();
buffer = new byte[2048];
bytes = ssl.Read(buffer, 0, 2048);
sb.Append(Encoding.ASCII.GetString(buffer), 0, bytes-1);
Console.WriteLine(">>>" + sb.ToString());
sb = new StringBuilder();
//}
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
}Решение задачи: «IMAP Почтовый клиент»
textual
Листинг программы
static void receiveResponse(string command)
{
try
{
if (command != "")
{
command = ("A" + tagNumber.ToString("D4") + " " + command + "\r\n");
Console.WriteLine("<<<" + command);
if (tcpc.Connected)
{
dummy = Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0, dummy.Length);
tagNumber++;
}
else
{
throw new ApplicationException("TCP CONNECTION DISCONNECTED");
}
}
bytes = -1;
buffer = new byte[2048];
sslReadStatus = -1;
while (sslReadStatus != 0)
{
try
{
bytes = ssl.Read(buffer, 0, buffer.Length);
}
catch (Exception)
{
break;
}
//Декодирование ответа сервера
sb.Clear();
sb.Append(Encoding.ASCII.GetString(buffer, 0, bytes-2));
switch (buffer[0])
{
case (byte)'*' : ParserStar(command); break;
case (byte)'A' : ParserCommand(); break; //A0001 OK
case (byte)'{' : ParserBuffer(); break;
case (byte)23 : continue; //23,3,1,0,160
case (byte)0 : continue; //\0,\0,\0
default : ParserDefault(); continue;
}
if (command == "") break;
}
}