使用 C# 向 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 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直在寻找这种“Hello World”示例来帮助我入门。在 dkarp 的回答的帮助下,这是我对 Miguel 的例子的看法:
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:
您可能会考虑使用固定的 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.
您的问题是您期望来自 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 theNOT AUTHENTICATED
state.