使用 C# 向 GMail 发送 IMAP 命令

发布于 2024-09-25 19:53:53 字数 2309 浏览 6 评论 0原文

我一直在尝试访问我的 GMail 帐户以从我的电子邮件帐户中检索未读电子邮件。但是,我只执行登录...之后的任何操作都不起作用。

首先,我连接到服务器,然后发送登录命令,最后发送检查命令。问题是收到的响应仅涉及连接和登录。之后,它就停止等待从 StreamReader 读取某些内容。

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);
            tcpclient.Connect("imap.gmail.com", 993);
            // This is Secure Stream // opened the connection between client and POP Server
            SslStream sslstream = new SslStream(tcpclient.GetStream());
            // authenticate as client  

            sslstream.AuthenticateAsClient("imap.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);

            sw.WriteLine("tag LOGIN [email protected] pass");
            sw.Flush();

            sw.WriteLine("tag2 EXAMINE inbox");
            sw.Flush();

            sw.WriteLine("tag3 LOGOUT ");
            sw.Flush();

            string str = string.Empty;
            string strTemp = string.Empty;
            try
            {
                while ((strTemp = reader.ReadLine()) != null)
                {
                    Console.WriteLine(strTemp);
                    // find the . character in line
                    if (strTemp == ".")
                    {
                        //reader.Close();
                        break;
                    }
                    if (strTemp.IndexOf("-ERR") != -1)
                    {
                        //reader.Close();
                        break;
                    }
                    str += strTemp;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            //reader.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

I've been trying to access my GMail account to retrieve the unread emails from my email account. However, I only con perform login... Anything after that doesn't work.

First of all I connect to the server, then send the login command and finally the examine command. The thing is that the responses that are receive refers only to the connection and to the login. After that, it just stops waiting for someting to read from the StreamReader.

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);
            tcpclient.Connect("imap.gmail.com", 993);
            // This is Secure Stream // opened the connection between client and POP Server
            SslStream sslstream = new SslStream(tcpclient.GetStream());
            // authenticate as client  

            sslstream.AuthenticateAsClient("imap.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);

            sw.WriteLine("tag LOGIN [email protected] pass");
            sw.Flush();

            sw.WriteLine("tag2 EXAMINE inbox");
            sw.Flush();

            sw.WriteLine("tag3 LOGOUT ");
            sw.Flush();

            string str = string.Empty;
            string strTemp = string.Empty;
            try
            {
                while ((strTemp = reader.ReadLine()) != null)
                {
                    Console.WriteLine(strTemp);
                    // find the . character in line
                    if (strTemp == ".")
                    {
                        //reader.Close();
                        break;
                    }
                    if (strTemp.IndexOf("-ERR") != -1)
                    {
                        //reader.Close();
                        break;
                    }
                    str += strTemp;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            //reader.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别靠近我心 2024-10-02 19:53:53

我一直在寻找这种“Hello World”示例来帮助我入门。在 dkarp 的回答的帮助下,这是我对 Miguel 的例子的看法:

        static void Main( string[] args ) {
        try {
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect( "imap.gmail.com", 993 );

            SslStream sslstream = new SslStream( tcpclient.GetStream() );
            sslstream.AuthenticateAsClient( "imap.gmail.com" );

            if ( sslstream.IsAuthenticated ) {
                StreamWriter sw = new StreamWriter( sslstream );
                StreamReader sr = new StreamReader( sslstream );

                sw.WriteLine( "tag LOGIN [email protected] pass" );
                sw.Flush();
                ReadResponse( "tag", sr );

                sw.WriteLine( "tag2 EXAMINE inbox" );
                sw.Flush();
                ReadResponse( "tag2", sr );

                sw.WriteLine( "tag3 LOGOUT" );
                sw.Flush();
                ReadResponse( "tag3", sr );
            }
        }
        catch ( Exception ex ) {
            Console.WriteLine( ex.Message );
        }
    }

    private static void ReadResponse( string tag, StreamReader sr ) {
        string response;

        while ( ( response = sr.ReadLine() ) != null ) {
            Console.WriteLine( response );

            if ( response.StartsWith( tag, StringComparison.Ordinal ) ) {
                break;
            }
        }
    }

I was looking for just this sort of "Hello World" example to get me started. With the help of dkarp's answers, here's my take on Miguel's example:

        static void Main( string[] args ) {
        try {
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect( "imap.gmail.com", 993 );

            SslStream sslstream = new SslStream( tcpclient.GetStream() );
            sslstream.AuthenticateAsClient( "imap.gmail.com" );

            if ( sslstream.IsAuthenticated ) {
                StreamWriter sw = new StreamWriter( sslstream );
                StreamReader sr = new StreamReader( sslstream );

                sw.WriteLine( "tag LOGIN [email protected] pass" );
                sw.Flush();
                ReadResponse( "tag", sr );

                sw.WriteLine( "tag2 EXAMINE inbox" );
                sw.Flush();
                ReadResponse( "tag2", sr );

                sw.WriteLine( "tag3 LOGOUT" );
                sw.Flush();
                ReadResponse( "tag3", sr );
            }
        }
        catch ( Exception ex ) {
            Console.WriteLine( ex.Message );
        }
    }

    private static void ReadResponse( string tag, StreamReader sr ) {
        string response;

        while ( ( response = sr.ReadLine() ) != null ) {
            Console.WriteLine( response );

            if ( response.StartsWith( tag, StringComparison.Ordinal ) ) {
                break;
            }
        }
    }
忆梦 2024-10-02 19:53:53

您可能会考虑使用固定的 IMAP/SSL 库 - 此处仍然处于活动状态。

此替代方案不是免费的。

其中之一的基础是可能有用的源代码因为您想推出自己的协议处理程序。

You might look at using a canned IMAP/SSL library instead - there is one that is still active here.

This alternative is not free.

The basis for one of these has source code that might be helpful since you want to roll your own protocol handler.

吻安 2024-10-02 19:53:53

您的问题是您期望来自 IMAP 服务器的 POP 响应。 POP 使用 . 终止获取的消息,并使用以 +OK-ERR 开头的行响应其他命令。 IMAP 没有。您正在消耗所有服务器响应,然后挂起,等待某些内容与类似 POP 的响应解析器相匹配。如果检查返回的数据,您应该会看到服务器对您的(格式正确的)请求的其余响应。

服务器有可能不会发回对您的第二个和第三个命令的响应。这可能是因为您正在尝试管道传输三个请求;也就是说,您无需等待响应即可发送请求。服务器有义务在SELECTED中允许管道传输状态,但协议不保证您可以从 NOT AUTHENTICATED 状态传输命令。

Your problem is that you're expecting POP responses from an IMAP server. POP terminates fetched messages with . and responds to other commands with a line beginning with either +OK or -ERR. IMAP doesn't. You're consuming all the server responses and then hanging, waiting for something to match your POP-like response parser. If you examine the returned data, you should see the remainder of the server's responses to your (properly-formatted) requests.

There is a possibility that the server isn't sending back responses to your second and third commands. This could be because you're trying to pipeline three requests; that is, you're sending the requests without waiting for the responses. The server is obliged to allow pipelining while in the SELECTED state, but the protocol doesn't guarantee that you can pipeline commands from the NOT AUTHENTICATED state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文