Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中使用pop3读取邮件中的邮件正文?_C# - Fatal编程技术网

如何在c#中使用pop3读取邮件中的邮件正文?

如何在c#中使用pop3读取邮件中的邮件正文?,c#,C#,这是代码: protected void Button9_Click(object sender, EventArgs e) { try { // create an instance of TcpClient TcpClient tcpclient = new TcpClient(); // HOST NAME POP SERVER and gmail uses port number 995 for POP t

这是代码:

protected void Button9_Click(object sender, EventArgs e)
{
    try
    {
        // 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();

        // RETR 1 will retrive your first email. it will read content of 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;
        }

        textbox1.text = str;
        textbox1.text += "<BR>" + "Congratulation.. ....!!! You read your first gmail email ";
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}
protectedvoid按钮9\u单击(对象发送者,事件参数e)
{
尝试
{
//创建TcpClient的实例
TcpClient TcpClient=新的TcpClient();
//主机名POP服务器和gmail使用端口号995进行POP
tcpclient.Connect(“pop.gmail.com”,995);
//这是安全流//已打开客户端和POP服务器之间的连接
System.Net.Security.SslStream SslStream=新的SslStream(tcpclient.GetStream());
//作为客户端进行身份验证
sslstream.com客户端(“pop.gmail.com”);
//bool flag=sslstream.IsAuthenticated;//检查标志
//指派编写器进行流式处理
System.IO.StreamWriter sw=新StreamWriter(SSL流);
//将读卡器分配到流
System.IO.StreamReader=新的StreamReader(sslstream);
//参考POP rfc命令,在6-9命令附近很少有
sw.WriteLine(“用户您的gmail用户_name@gmail.com");
//发送到服务器
sw.Flush();sw.WriteLine(“传递您的gmail密码”);
sw.Flush();
//RETR 1将检索您的第一封电子邮件。它将读取您的第一封电子邮件的内容
西南写入线(“RETR 1”);
sw.Flush();
//关闭连接
sw.WriteLine(“退出”);
sw.Flush();string str=string.Empty;
string strTemp=string.Empty;
而((strTemp=reader.ReadLine())!=null)
{
//在第行中查找.character
如果(strTemp==”)
{
打破
}
if(strTemp.IndexOf(“-ERR”)!=-1)
{
打破
}
str+=strTemp;
}
textbox1.text=str;
textbox1.text+=“
”+“祝贺你……!!!你读了第一封gmail邮件”; } 捕获(例外情况除外) { 响应。写入(例如消息); } }

消息体是一组看似随机的字符。我知道这不仅仅是一堆随机字符,而是一些需要解析和转换的代码。如何阅读“邮件正文”中的内容?

我知道我不是直接回复您的答案,但阅读电子邮件是一项非常复杂的任务,我认为您可以通过外部库而不是自己实现它,更好更快地实现这一点

有很多很好的实现,我通常使用OpenPop.NET,它运行良好,并且是开源的

你们可以在网上找到很多例子,因为它非常流行

您可以轻松获取所有邮件:

using(Pop3Client client = new Pop3Client())
{
    // Connect to the server
    client.Connect("pop.gmail.com", 995, true);

    // Authenticate ourselves towards the server
    client.Authenticate("username@gmail.com", "password", AuthenticationMethod.UsernameAndPassword);

    // Get the number of messages in the inbox
    int messageCount = client.GetMessageCount();

    // We want to download all messages
    List<Message> allMessages = new List<Message>(messageCount);

    // Messages are numbered in the interval: [1, messageCount]
    // Ergo: message numbers are 1-based.
    // Most servers give the latest message the highest number
    for (int i = messageCount; i > 0; i--)
    {
        allMessages.Add(client.GetMessage(i));
    }
}
或者,如果是utf8编码的电子邮件:

var encodedStringAsBytes = System.Convert.FromBase64String(message.RawMessage); 
var rawMessage =System.Text.Encoding.UTF8.GetString(encodedStringAsBytes);
相反,如果您只想要邮件正文,则必须深入邮件结构:


我知道这不是一项简单的任务,但正如我前面所说,电子邮件是复杂的对象。

为什么不使用OpenPop.Net?你能解释一下关于“OpenPop.Net”的更多信息吗?这是什么?我如何使用它?谢谢。我用过这个方法(var-mailbody=ascienceoding.ASCII.GetString(message.RawMessage);),但是当我在邮件正文中阅读邮件时,内容非常混乱,我看不懂这个内容。就像这样:(b=uGcdcfXGcFZ/bahnxzdmltopyyyg/d0lkaTlBu0UHT9eNjaxcqIGitxeHFGus5H0uwm G+S7P/yphixo8kr4cyxkamlvqs3si/dys8gjylxUbFxNumnkir35ayeuv8j8)。那么,我该怎么做才能阅读文本呢?@huyleuit这是在发送邮件时utf8@huyleuit你在使用openpop.net吗?
var encodedStringAsBytes = System.Convert.FromBase64String(message.RawMessage); 
var rawMessage =System.Text.Encoding.UTF8.GetString(encodedStringAsBytes);