C# 使用C向GMail发送IMAP命令#
我一直在尝试访问我的GMail帐户,从我的电子邮件帐户中检索未读的电子邮件。但是,我只能执行登录。。。在那之后的任何事情都不起作用 首先,我连接到服务器,然后发送login命令,最后发送examine命令。问题是,接收的响应只涉及连接和登录。在那之后,它只是停止等待从StreamReader读取某些内容C# 使用C向GMail发送IMAP命令#,c#,email,ssl,gmail,imap,C#,Email,Ssl,Gmail,Imap,我一直在尝试访问我的GMail帐户,从我的电子邮件帐户中检索未读的电子邮件。但是,我只能执行登录。。。在那之后的任何事情都不起作用 首先,我连接到服务器,然后发送login命令,最后发送examine命令。问题是,接收的响应只涉及连接和登录。在那之后,它只是停止等待从StreamReader读取某些内容 try { // create an instance of TcpClient TcpClient tcpclient = ne
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);
}
您可能会考虑使用一个固定的IMAP/SSL库——有一个仍然处于活动状态 这不是免费的
其中一个的基础是,您希望滚动您自己的协议处理程序。您的问题是,您期望来自IMAP服务器的POP响应。POP以
终止获取的消息,并以+OK
或-ERR
开头的行响应其他命令。IMAP没有。您正在使用所有服务器响应,然后挂起,等待与类似POP的响应解析器匹配的内容。如果检查返回的数据,您应该会看到服务器对您的(正确格式化的)请求的其余响应
服务器可能没有向您的第二个和第三个命令发回响应。这可能是因为您试图通过管道传输三个请求;也就是说,您在不等待响应的情况下发送请求。服务器是,但协议不能保证您可以从
未验证状态
管道化命令。我正在寻找这种“Hello World”示例来开始。借助于dkarp的回答,以下是我对米格尔例子的看法:
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 user@gmail.com 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;
}
}
}
我已经知道这两个,但我的主要目标是学习如何与IMAP服务器通信……而且,
注销
命令中的尾随空格是不允许的,并且可能会导致tag3错误…
响应。(可能会晚一点,但最终是协议级的答案…)