-2

我正在尝试从电子邮件中提取标题和正文信息,以下代码以原始形式检索标题和正文。我有一个包含字段fromsubjectdate和的电子邮件对象body。我想从电子邮件中提取这些值并将它们分配给电子邮件对象。我该如何解决?我尝试了几种方法,例如获取标题信息并使用 astreamReader.ReadLine()来获取一条线,但我得到了illegal path exceptions. 我知道我可以使用库,但我需要以这种方式实现它。

我的意思是,IMAP 命令返回标头信息。我想提取subject值、date值、sendere-amil 等并将它们分配给我的电子邮件对象对应的值,例如

emailObject.subject = "subjectValue"


public class Imap
{

    static void Main(string[] args)
    {
        try
        {
            path = Environment.CurrentDirectory + "\\emailresponse.txt";

            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);

            sw = new System.IO.StreamWriter(System.IO.File.Create(path));

            tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);

            ssl = new System.Net.Security.SslStream(tcpc.GetStream());
            ssl.AuthenticateAsClient("imap.gmail.com");
            receiveResponse("");

            Console.WriteLine("username : ");
            username = Console.ReadLine();

            Console.WriteLine("password : ");
            password = Console.ReadLine();
            receiveResponse("$ LOGIN " + username + " " + password + "  \r\n");
            Console.Clear();

            receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");

            receiveResponse("$ SELECT INBOX\r\n");

            receiveResponse("$ STATUS INBOX (MESSAGES)\r\n");


            Console.WriteLine("enter the email number to fetch :");
            int number = int.Parse(Console.ReadLine());

            Console.WriteLine("*************Header************");
            Console.WriteLine("");
           // receiveResponse("$ FETCH " + number + " body[header]\r\n");
           // BODY.PEEK[HEADER.FIELDS (SUBJECT)]

         //   StringBuilder sb = receiveResponse("$ FETCH " + number + " BODY.PEEK[HEADER.FIELDS (From Subject Date)]\r\n");
          StringBuilder sb= receiveResponse("$ FETCH " + number + " body.peek[header]\r\n");

             Console.WriteLine(sb);

            Console.WriteLine("");
            Console.WriteLine("Body");
            sb = new StringBuilder();
            sb=receiveResponse("$ FETCH " + number + " body[text]\r\n");

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] serverbuff = new Byte[1024];
            int count = 0;
            string retval = enc.GetString(serverbuff, 0, count);

            Console.WriteLine(sb.ToString());

            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();
            }
        }


        Console.ReadKey();
    }
    static StringBuilder receiveResponse(string command)
    {
        sb = new StringBuilder();
        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[2048];
            bytes = ssl.Read(buffer, 0, 2048);
            sb.Append(Encoding.ASCII.GetString(buffer));


          //  Console.WriteLine(sb.ToString());
            sw.WriteLine(sb.ToString());
           // sb = new StringBuilder();

            return sb;

        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    }
4

1 回答 1

2

您说您不想使用 IMAP 库。这意味着您将必须实现自己的。您应该从阅读RFC 3501开始,因为如果不仔细阅读文档,您就不可能获得正确的协议。特别是,您正在STATUS对当前选定的邮箱发出命令,这是协议规范明确禁止的。其余代码支持您尚未阅读 RFC 的假设。

于 2013-04-22T15:39:40.573 回答