【问题标题】:TCPClient connects to Gmail but does not receive first messageTCPClient 连接到 Gmail 但未收到第一条消息
【发布时间】:2012-10-11 04:44:36
【问题描述】:

我正在使用此代码连接到 gmail。每个 WriteLine() 都被写入。那么大概一切正常吗?但是,没有消息写入控制台。怎么了? 这是我的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net.Security;

namespace RemoteControl
{
class MailClient
{
    static void Main(string[] args)
    {
        try
        {
            // create an instance of TcpClient 
            Console.WriteLine("Connecting...");
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect("imap.gmail.com", 993);
            SslStream sslstream = new SslStream(tcpclient.GetStream());
            sslstream.AuthenticateAsClient("imap.gmail.com");
            Console.WriteLine("Reached Gmail.");
            StreamWriter sw = new StreamWriter(sslstream);
            System.IO.StreamReader reader = new StreamReader(sslstream);
            Console.WriteLine("Sending username.");
            sw.WriteLine("USER user@gmail.com");
            Console.WriteLine("Sending password.");
            sw.WriteLine("PASS pass");
            Console.WriteLine("Receiving Message.");
            sw.WriteLine("RETR 1");
            Console.WriteLine("Complete.");
            tcpclient.Close(); // close the connection
            Console.ReadLine();
        }
        catch (IOException e) 
        {
            Console.WriteLine(e);
        }
    }
}
}

【问题讨论】:

  • 1.流、流读取器和流写入器(可能还有 tcpclient)应该在 using 块中。 2. 如果抛出不同的异常怎么办? 3.finally {Console.ReadLine();}
  • 您的读者似乎从未真正阅读过任何内容。
  • Err,调用读取方法?不是一个真正的问题。

标签: c# ssl gmail tcpclient gmail-imap


【解决方案1】:

** 根据我的理解,您的问题是希望使用 C# 阅读电子邮件。如果是真的,我认为 socket pop3 是解决您问题的好选择。例如:**

using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
public partial class Default3 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
{
    // create an instance of TcpClient 
    TcpClient tcpclient = new TcpClient();
    tcpclient.Connect("pop3.live.com", 995);
    System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
    sslstream.AuthenticateAsClient("pop3.live.com");
    StreamWriter sw = new StreamWriter(sslstream);
    System.IO.StreamReader reader = new StreamReader(sslstream);
    sw.WriteLine("USER xxx@live.com"); sw.Flush();
    sw.WriteLine("PASS xxxx****"); sw.Flush();
    sw.WriteLine("RETR 1");
    sw.Flush();
    sw.WriteLine("Quit ");
    sw.Flush();
    string str = string.Empty;
    string strTemp = string.Empty;
    while ((strTemp = reader.ReadLine()) != null)
    {
        if (".".Equals(strTemp))
        {
            break;
        }
        if (strTemp.IndexOf("-ERR") != -1)
        {
            break;
        }
        str += strTemp;
    }
    Response.Write(str);
    reader.Close();
    sw.Close();
    tcpclient.Close(); // close the connection
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2022-10-01
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多