C# 4.0 如何从Gmail获得电子邮件

C# 4.0 如何从Gmail获得电子邮件,c#-4.0,tcp,pop3,C# 4.0,Tcp,Pop3,我构建了以下代码,它打开了tcp连接,能够通过tcp向远程gmail服务器发送消息 TextWriter writerlog = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "service.txt"); writerlog.WriteLine("Initializing service"); TcpClient tcpclient = new TcpClient(); // c

我构建了以下代码,它打开了tcp连接,能够通过tcp向远程gmail服务器发送消息

TextWriter writerlog = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "service.txt");
            writerlog.WriteLine("Initializing service");
            TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient
            try
            {
                writerlog.WriteLine("Starting Tcp Client");

                writerlog.WriteLine("Connecting Tcp Client");

                tcpclient.Connect("pop.gmail.com", 995); // HOST NAME POP SERVER and gmail uses port number 995 for POP 

                writerlog.WriteLine("Getting stream from client");

                System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); // This is Secure Stream // opened the connection between client and POP Server

                sslstream.AuthenticateAsClient("pop.gmail.com"); // authenticate as client 

                writerlog.WriteLine("Creating stream writer and stream reader");

                StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream

                StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream

                writerlog.WriteLine("Init Authentication");

                writerlog.WriteLine("Sending user");

                SendCommand("USER xxxxxxxx@gmail.com", sw, reader); // refer POP rfc command, there very few around 6-9 command 

                writerlog.WriteLine("Sending password");

                SendCommand("PASS yyyyyyyyy", sw, reader); // Sending password 

                writerlog.WriteLine("Retrieving last email");

                writerlog.WriteLine("Email contents:" + SendCommand("RETR 1", sw, reader)); // this will retrive your first email

                writerlog.WriteLine("Closing connection");

                SendCommand("Quit", sw, reader);// close the connection
            }
            catch (Exception ex)
            {
                writerlog.WriteLine("Exception: " + ex.Message);
                writerlog.WriteLine("StackTrace: " + ex.StackTrace);
                writerlog.WriteLine("InnerExceptiion: " + ex.StackTrace);
            }
            finally 
            {
                writerlog.WriteLine("Ending service");
                tcpclient.Close();
                writerlog.Close();
            }
发送tcp消息的方法:

protected string SendCommand(string cmdtext, StreamWriter writer, StreamReader reader)
        {
            writer.WriteLine(cmdtext);
            writer.Flush();
            return reader.ReadLine();
        }
我有以下Pop3命令列表,并且我使用RETR来使用此行获取电子邮件内容:

writerlog.WriteLine(“电子邮件内容:+SendCommand(“RETR 1”,sw,reader))

但在我的日志文件中,我得到以下信息:

电子邮件内容:+确定发送通行证

    USER - Takes one argument i.e., the email address of the user trying to connect to his/her mailbox. Example usage: 
        USER youremail@xyz.com
    PASS - Takes one argument i.e., the password of the user. Example usage: 
        PASS yourpassword
    STAT - Returns the number of emails in the mailbox and the number of bytes all the emails are taking on the server. Example usage: 
        STAT
    TOP - Takes two arguments i.e., the sort number of the email on the server and the number of lines of text to retrieve from the body of the email. Example usage: 
        TOP 1 10
    RETR - Takes one argument i.e., the sort number of the email on the server and returns all the headers and lines from the body of the email. Example usage: 
        RETR 1
    DELE - Takes one argument i.e., the sort number of the email on the server and deletes it. Example usage: 
        DELE 1
    RSET - Resets any DELE commands given above. The emails marked to be deleted by DELE command are unmarked. Example usage: 
        RSET
    QUIT - Closes the user session with the server. Example usage: 
        QUIT
你能告诉我我在获取电子邮件内容时遗漏了什么吗

提前谢谢

亲切的问候

Jose.

这篇文章()解释了一种更好的方法来做你想做的事情。我用的是TCP方式。。但是使用Pop客户端要容易得多

致以最良好的祝愿


Yan.

您可以使用Imap阅读gmail:

ImapClient ic = new ImapClient("imap.gmail.com", "xxxxx@gmail.com", "xxxxxx",
            ImapClient.AuthMethods.Login, 993, true);           
        ic.SelectMailbox("INBOX");          
        int n = ic.GetMessageCount();
        textBox1.Text = n.ToString();
        MailMessage mail = ic.GetMessage(n-1);
        ic.Dispose();
        string sub = mail.Subject;
        string msgbody = mail.Raw;
或遵循pop3方法:

 string result = "";
        string str = string.Empty;
        string strTemp = string.Empty;

        try
        {                
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect("pop3.gmail.com", 995);
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
            sslstream.AuthenticateAsClient("pop3.gmail.com");
            System.IO.StreamWriter sw = new StreamWriter(sslstream);
            System.IO.StreamReader reader = new StreamReader(sslstream);
            strTemp = reader.ReadLine();
            sw.WriteLine("USER" + " " + username);
            sw.Flush();
            strTemp = reader.ReadLine();
            sw.WriteLine("PASS" + " " + password);
            sw.Flush();
            strTemp = reader.ReadLine();                
                string[] numbers = Regex.Split(strTemp, @"\D+");
                int a = 0;
                foreach (string value in numbers)
                {
                    if (!string.IsNullOrEmpty(value))
                    {

                        int i = int.Parse(value);
                        numbers[a] = i.ToString();
                        a++;
                    }
                }                                    
            sw.WriteLine("RETR "+numbers[0]);
            sw.Flush();
            strTemp = reader.ReadLine();
            textBox4.Text = strTemp;
            while ((strTemp = reader.ReadLine()) != null)
            {
                if (strTemp == ".")
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }
            sw.WriteLine("Quit ");
            sw.Flush();                                
            this.sourceHTML = str;
         }
        catch () {}

ImapClient类位于何处?我找不到