IMAP 响应不正确

发布于 2024-11-02 08:22:52 字数 1202 浏览 2 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2024-11-09 08:22:52
  1. 不要使用字符数组:

    byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n");

  2. TCP 是基于的,而不是基于消息的。

这意味着无法保证通过相同的 Read 接收到整个消息。 读取可以接收半条消息、完整消息或两条消息。您需要进行相应的处理。

3 使用 return Encoding.ASCII.GetString(data, 0, ret);

4 有几个开源 IMAP 库。既然您是套接字编程新手,为什么不使用其中之一呢?

  1. Do not use char array:

    byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes("$ UID FETCH " + index + " (BODYSTRUCTURE)" + "\r\n");

  2. TCP is stream based and not message based.

This means that nothing guarantees that the entire message is received with the same Read. A Read 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文