使用C#发送IMAP命令的GMail [英] Send IMAP commands to GMail using C#

查看:411
本文介绍了使用C#发送IMAP命令的GMail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图访问我的Gmail帐户检索我的电子邮件帐户的未读邮件。不过,我只CON进行登录...任何后不起作用。



我所有连接到服务器的第一,然后发送login命令,最后检查命令。问题是,那些接收响应仅指连接和登录。在此之后,它只是停止等待成才从StreamReader的阅读。

 
{
/ /创建TcpClient的
TcpClient的TcpClient的实例=新的TcpClient();
//主机名POP服务器和Gmail使用端口号995 POP
//tcpclient.Connect(\"pop.gmail.com,995);
tcpclient.Connect(imap.gmail.com,993);
//这是安全流//打开客户端和服务器POP
SslStream sslstream之间的连接=新SslStream(tcpclient.GetStream());
//为客户

sslstream.AuthenticateAsClient(imap.gmail.com)进行身份验证;

布尔标志= sslstream.IsAuthenticated; //检查标记
// Asssigned作家流
System.IO.StreamWriter SW =新的StreamWriter(sslstream);
//分配读者流
就是System.IO.StreamReader读者=新的StreamReader(sslstream);

sw.WriteLine(标记LOGIN user@gmail.com通行证);
sw.Flush();

sw.WriteLine(标签2 EXAMINE收件箱);
sw.Flush();

sw.WriteLine(TAG3注销);
sw.Flush();

字符串str =的String.Empty;
字符串strTemp =的String.Empty;

{
,而((strTemp = reader.ReadLine())!= NULL)
{
Console.WriteLine(strTemp);
//找到。字在线路
如果(。strTemp ==)
{
//reader.Close();
中断;
}
如果(strTemp.IndexOf( - ERR)!= -1)
{
//reader.Close();
中断;
}
海峡+ = strTemp;
}
}
赶上(异常前)
{
字符串s = ex.Message;
}
//reader.Close();

}
赶上(异常前)
{
Console.WriteLine(ex.Message);
}


解决方案

您可以看看使用罐装IMAP / SSL库,而不是 - 还有一个仍然是活动的这里



这种替代是不是免费的。



有关这些中的一个基础具有的源代码,因为你想推出自己的协议处理程序可能会有所帮助


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 user@gmail.com 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);
        }

解决方案

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.

这篇关于使用C#发送IMAP命令的GMail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆