C# POP3协议。操作统计和列表
我尝试实现简单的邮件客户端。现在我可以检索邮件:C# POP3协议。操作统计和列表,c#,tcpclient,pop3,C#,Tcpclient,Pop3,我尝试实现简单的邮件客户端。现在我可以检索邮件: // create an instance of TcpClient TcpClient tcpclient = new TcpClient(); // HOST NAME POP SERVER and gmail uses port number 995 for POP tcpclient.Connect("pop.gmail.com", 995)
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.gmail.com", 995);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("pop.gmail.com");
//bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER my_mail@gmail.com");
// sent to server
sw.Flush();
sw.WriteLine("PASS my_pass");
sw.Flush();
// this will retrive your first email
sw.WriteLine("RETR 1");
sw.Flush();
string str = string.Empty;
string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
//str = reader.ReadToEnd();
// close the connection
sw.WriteLine("QUIT");
sw.Flush();
richTextBox2.Text = str;
但是当我试图实现操作STAT
和LIST
时,我的程序崩溃了。我认为在阅读流的循环中有一个问题。对于操作STAT
,我尝试读取,直到“\r\n”
(strTemp=“\r\n”
)为止,对于操作列表
-”\r\n“
这是我的STAT
代码:
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.gmail.com", 995);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("pop.gmail.com");
//bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER my_mail@gmail.com");
// sent to server
sw.Flush();
sw.WriteLine("PASS my_pass");
sw.Flush();
// this will retrive your first email
sw.WriteLine("STAT");
sw.Flush();
string str = string.Empty;
string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == "\r\n")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
//str = reader.ReadToEnd();
// close the connection
sw.WriteLine("QUIT");
sw.Flush();
richTextBox2.Text = str;
按下按钮后,我的主窗口没有响应。我的错在哪里
谢谢大家! 您的应用程序很可能挂在
ReadLine()
上。请注意,其中不包括\r\n
。因此,您对\r\n
的检查将永远不会命中,因此break语句将永远不会命中
如果(strTemp==“”),您可能只需要将其更改为。如果这不起作用,则必须在调试器中逐步执行
还要注意,在UI线程中阻止这样的调用不是一个好主意。你真的应该把它交给后台工作人员。你忽略现有库的任何有效原因,例如?什么是“崩溃”异常(因为这是CLR,所以没有GPF,是吗)?你添加了什么代码导致它?你知道,这里的心灵感应者不多。@ivan_pozdeev,我编辑了我的问题。这是我实验室的任务,因此我无法在项目中导入库。我是按照规范做的,不知道为什么不起作用。有什么想法吗?Jon B,没有,没用。我也尝试了if(strTemp==null)
。我也不知道为什么我不能清除streamreader.DiscardBufferedData()代码>发送我的登录名和密码后