c# - 使用 C# 向 GMail 发送 IMAP 命令

标签 c# email ssl gmail imap

我一直在尝试访问我的 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9b9d8b9cae89838f8782c08d8183" rel="noreferrer noopener nofollow">[email protected]</a> 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);
        }

最佳答案

我一直在寻找这种“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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c6c0d6c1f3d4ded2dadf9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a> 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;
            }
        }
    }

关于c# - 使用 C# 向 GMail 发送 IMAP 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863381/

相关文章:

c# - iOS 不支持全局 PushAsync,请使用 NavigationPage

c# - ASP.NET 中的 session 超时警告

javascript - Sendgrid Node js 不发送电子邮件。程序没有错误信息

php - 如何向多个收件人发送电子邮件?

ssl - 永久 SEO 安全 301 从 HTTP 重定向到 HTTPS 影响别名

c# - 如何从接受输入的 C# 表单应用程序打开 cmd

c# - 为 Windows Mobile、Android 和 iPhone 开发 C# 应用程序

bug-tracking - 具有良好电子邮件集成和体面导航的票务跟踪软件?

rest - Restful 服务上的数据加密

html - 使用托管在其他站点上的 SSL 图像有任何风险吗?