我在使用IMAP阅读电子邮件时获得原始文本

本文关键字:原始 文本 电子邮件 IMAP | 更新日期: 2023-09-27 18:19:17

我已经采取了一些代码从MSDN使用IMAP客户端读取电子邮件。我已经改变了一点点代码,所以我只能阅读看不见的电子邮件。我在Richtextbox中写所有的响应。

问题是电子邮件的正文格式是不可读的,而所有其他文本是好的。

    void ReadEmail()
    {
        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.gmail.com", 993);
            ssl = new System.Net.Security.SslStream(tcpc.GetStream());
            ssl.AuthenticateAsClient("imap.gmail.com");
            receiveResponse("");

            username = "charlie@gmail.com";
            password = "********";
            receiveResponse("$ LOGIN " + username + " " + password + "  'r'n");
            receiveResponse("$ LIST " + "'"'"" + " '"*'"" + "'r'n");
            receiveResponse("$ SELECT INBOX'r'n");
            receiveResponse("$ UID SEARCH UNSEEN'r'n");
           MatchCollection collection= Regex.Matches(Result,@" ('d{1,4})");
           foreach (Match m in collection)
           { 
           UNREAD_UID.Add(int.Parse(m.Groups[1].Value));
           }
            foreach (int x in UNREAD_UID)
            {
                     receiveResponse("$ FETCH " +x + " body[header]'r'n");
                    richTextBox1.Text += Environment.NewLine+"-----------------------------------------------------------------------------------------------------------------------"+Environment.NewLine;
                    receiveResponse("$ FETCH " +x + " body[text]'r'n");
                    richTextBox1.Text += Environment.NewLine + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2" + Environment.NewLine;
                    richTextBox1.Update();
            }

            //receiveResponse("$ STATUS INBOX (MESSAGES)'r'n");

          //  int number = 1;

            receiveResponse("$ LOGOUT'r'n");
        }
        catch (Exception ex)
        {
            Console.WriteLine("error: " + ex.Message);
        }
        finally
        {
            if (sw != null)
            {
                sw.Close();
                sw.Dispose();
            }
            if (ssl != null)
            {
                ssl.Close();
                ssl.Dispose();
            }
            if (tcpc != null)
            {
                tcpc.Close();
            }
        }
     }

   void receiveResponse(string command)
    {
        try
        {
            if (command != "")
            {
                if (tcpc.Connected)
                {
                    dummy = Encoding.ASCII.GetBytes(command);
                    ssl.Write(dummy, 0, dummy.Length);
                }
                else
                {
                    throw new ApplicationException("TCP CONNECTION DISCONNECTED");
                }
            }
            ssl.Flush();
            buffer = new byte[5120];
            bytes = ssl.Read(buffer, 0, 5120);
            sb.Append(Encoding.ASCII.GetString(buffer));
            Result = sb.ToString();
            richTextBox1.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + sb.ToString();
            sb = new StringBuilder();
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    } 

这是我得到的样本

108 FETCH (BODY[TEXT] {25656}DQoNCg0KDQo8IURPQ1RZUEUgaHRtbD4NCjxodG1sPg0KPGhlYWQ + DQo8dGl0bGU + TWljcm9zb2Z0IHR1cm5zIChhbG1vc3QpIGFueSBjYW1lcmEgaW50byBhIEtpbmVjdDwvdGl0bGU + DQo8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD1VVEYtOCI + DQo8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoIj4NCjwhLS0gRWZmaW5nIFdpbmRvd3MgOCBNYWlsIGNsaWVudC4gQWRkIC13ZWJraXQtbWluLWRldmljZS1waXhlbC1yYXRpbzogMSB0byBzY2FyZSBpdCBhd2F5IGZyb20gdGhpcyBiaXQuIC0tPg0KPHN0eWxlIHR5cGU9InRleHQvY3NzIj4NCkBtZWRpYSBzY3JlZW4gYW5kIChtYXgtZGV2aWNlLXdpZHRoOiA1MDBweCkgYW5kICgtd2Via2l0LW1pbi1kZXZpY2UtcGl4ZWwtcmF0aW86IDEpIHsNCgkuYm9keS1jb250YWluZXIgeyB3aWR0aDoxMDAlICFpbXBvcnRhbnQ7IH0NCgkuYmFubmVyICAgICAgICAgeyBkaXNwbGF5OiBub25lICFpbXBvcnRhbnQ7IH0NCgkubW9iaWxlLWJhbm5lciAgeyBkaXNwbGF5OiBibG9jayAhaW1wb3J0YW50OyB3aWR0aDozNDBweCAhaW1wb3J0YW50OyBiYWNrZ3JvdW5kOiB1cmwoaHR0cDovL3d3dy5jb2RlcHJvamVjdC5jb20vc2NyaXB0L21haWxvdXRzL3RlbXBsYXRlcy9uZXdzbGV0dGVyLWluc2lkZXIucG5nKSBuby1yZXBlYXQgdG9wIGxlZ

请帮帮我。

我在使用IMAP阅读电子邮件时获得原始文本

您需要检查Content-Transfer-Encoding报头来撤消传输编码(在本例中是Base64)。其他选择是7位或引用可打印)。或者,更好的方法是下载整个消息(Body[])并对其应用MIME解析器/解码器,以获得报头、正文和附件的对象表示。

Max上面的答案是正确的,但是我将演示如何使用我的MailKit库实现他的建议:

using (var client = new ImapClient ()) {
    client.Connect ("imap.gmail.com", 993, true);
    // since we're not using an OAuth2 token, remove it from the set
    // of possible authentication mechanisms to try:
    client.AuthenticationMechanisms.Remove ("XOAUTH2");
    client.Authenticate ("charlie@gmail.com", "*****");
    // SELECT the INBOX folder
    client.Inbox.Open (FolderAccess.ReadWrite);
    foreach (var uid in client.Inbox.Search (SearchQuery.NotSeen)) {
        var message = client.Inbox.GetMessage (uid);
        // at this point, 'message' is a MIME DOM that you can walk
        // over to get the particular MIME-part that you want. For
        // example, we could get a body part with a filename of
        // "test.txt" using LINQ like this:
        var attachment = message.BodyParts.OfType<MimePart> ()
            .FirstOrDefault (x => x.FileName == "test.txt");
        // decode the content to a MemoryStream:
        using (var memory = new MemoryStream ()) {
            attachment.ContentObject.DecodeTo (memory);
        }
        // since the attachment is probably a TextPart
        // (based on the file extension above), we can actually
        // use a simpler approach:
        var textPart = attachment as TextPart;
        if (textPart != null) {
            // decode the content and convert into a 'string'
            var text = textPart.Text;
        }
    }
    client.Disconnect (true);
}