C# 使用TcpClient类的邮件客户端
我找到了从邮箱接收邮件的示例:C# 使用TcpClient类的邮件客户端,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
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER your_gmail_user_name@gmail.com");
// sent to server
sw.Flush();
sw.WriteLine("PASS your_gmail_password");
sw.Flush();
// this will retrive your first email
sw.WriteLine("RETR 1");
sw.Flush();
// close the connection
sw.WriteLine("Quit ");
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字符串。 但是我认为你应该在发送到服务器之前阅读邮件。所以把你的环放在前面
// close the connection
sw.WriteLine("Quit ");
sw.Flush();
正如我在回答另一个几乎相同的问题时所写的:如果您希望编写邮件客户端,那么您确实需要阅读MIME规范和POP3规范 或者你可以使用一个为你做了大部分艰苦工作的图书馆,比如和
你仍然需要至少对MIME有一个基本的了解,但是,至少你不必为它编写解析器,这是非常重要的,因为现实世界中有多少消息不符合规范——通常是由那些从未真正阅读过规范的人编写的。当while循环完成时,str的值是多少?为什么你要发布两次问题?使用与您开始发布代码@M Patel相同的线程,这是另一个程序。来自上一个线程的程序包含错误,我不知道如何修复它们。这是另一个代码,谢谢。它起作用了!但我怎样才能将正文和主题以及其他有关邮件的信息分开呢?据我所知,在SMTP邮件中,您有一个标题,由邮件以2 CRLF分隔。最后,你的信息将以。。这意味着您可以按\r\n\r\n拆分并剥离该文件。最后。