IMAP 响应不正确
我正在开发 C# MailClient,它遵循 IMAP 协议,但我收到错误的响应,这意味着响应会给我一些重复的结果。 就像我第一次发送这样的命令一样。
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODY[HEADER.FIELDS (SUBJECT FROM DATE)])\r\n"));
我第二次这样发送。
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n"));
所以我再次得到第一个命令结果两次或两次以上; 有时它会继续给我第一个结果。
我的 Response() 方法是这样的。
private string Response()
{
string response = string.Empty;
byte[] data = new byte[_imapClient.ReceiveBufferSize];
int ret = _imapNs.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data,0,ret);
return response;
}
_imapClient 是一个对象
private TcpClient _imapClient;
,我正在获取 _imapClient 的值,如下所示。
public string GetMessageBodyStructure(int index)
{
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n"));
_imapNs.Write(commandBytes, 0, commandBytes.Length);
_imapNs.Flush();
return Response();
}
有说错的地方请指正谢谢..
I am working on C# MailClient and which is follow IMAP Protocol, but I am getting wrong response that means the response will give me some repetitive resul.
Like say first time I am sending command like this.
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODY[HEADER.FIELDS (SUBJECT FROM DATE)])\r\n"));
and second time I am sending like this.
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n"));
so I am getting again first command result in twice or more than 2 times;
and some times it's continue giving me first result.
my Response() method is like this.
private string Response()
{
string response = string.Empty;
byte[] data = new byte[_imapClient.ReceiveBufferSize];
int ret = _imapNs.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data,0,ret);
return response;
}
_imapClient is a object of an
private TcpClient _imapClient;
and I am taking value of _imapClient is like this.
public string GetMessageBodyStructure(int index)
{
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n"));
_imapNs.Write(commandBytes, 0, commandBytes.Length);
_imapNs.Flush();
return Response();
}
Where I am wrong correct me thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用字符数组:
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n");
TCP 是基于流的,而不是基于消息的。
这意味着无法保证通过相同的
Read
接收到整个消息。读取
可以接收半条消息、完整消息或两条消息。您需要进行相应的处理。3 使用
return Encoding.ASCII.GetString(data, 0, ret);
4 有几个开源 IMAP 库。既然您是套接字编程新手,为什么不使用其中之一呢?
Do not use char array:
byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n");
TCP is stream based and not message based.
This means that nothing guarantees that the entire message is received with the same
Read
. ARead
may receive a half message, a complete message or two messages. You need to handle that accordingly.3 Use
return Encoding.ASCII.GetString(data, 0, ret);
4 There are several open source IMAP libraries out there. Why not use one of those since you are new to socket programming?