C# IMAP 客户端 - 与邮件服务器的多个连接
在尝试了许多 IMAP API 之后,我决定编写自己的 API(大多数都是内存消耗大的,有些只是不起作用,内存不足异常等等)。
无论如何,我已经编写了一些有效的代码(使用 TCPClient 对象)并且到目前为止效果很好。但是,我希望我的邮件服务器能够处理对邮件服务器的多个请求。例如: 假设我获得了所有 UID 的列表,然后我循环浏览此列表,为每条消息获取我想要的内容(正文、标头等)。
问题是,我如何同时处理多个请求?因此,我可以一次处理 10 个 UID,而不是一次循环一个 UID。
这里最好的方法是什么?一组 TCP 客户端,每个客户端都有自己的线程?
谢谢。
After experimenting with many IMAP API's, I have decided to write my own (Most are memory hogs, some just do not work, out of memory exception etc etc etc).
Anyway I have wrote some code that works (using TCPClient object) and its good so far. However, I want my one to be able to handle multiple requests to the mail server. For example:
Say I get a list of all the UIDs, then I cycle through this list getting what I want (Body, Header, etc) for each message.
The question is, how would I handle, multiple requests at the same time? So instead of looping through the UIDs one at a time I can instead process 10 at a time.
What would be the best approach here? An array of TCP Clients, each with its own thread?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,建议 IMAP 客户端始终最多只有一个到服务器的连接。额外的连接不仅需要服务器上的宝贵资源,而且更重要的是,IMAP 规范不保证两个连接可以同时选择同一个邮箱。依赖服务器上存在的此功能可能会使您的客户端与这些服务器不兼容。
相反,您应该尽可能高效地使用该协议。请注意,许多命令可以对一组或一系列 UID 进行操作。这允许您在指定每个 UID 的情况下发出一个单一请求,而不是分别为每个 UID 发出一个请求。
另一个好的做法是请求的数据不要超过当前所需的数据。例如,假设您有一个消息列表。然后,不要请求所有消息的详细信息,仅请求当前可见消息的信息。
我强烈建议您阅读 RFC 2683,IMAP4 实施建议。它涵盖了这一点以及其他内容。
如果您决定无论如何使用多个连接,那么一个好的方法通常是使用异步操作,而不是显式使用单个线程。与某种运行循环集成的组合通常也很有用,这样当有数据要读取时就会调用您的代码,而不是您的代码必须轮询或显式检查它。即使您只使用单个连接,这通常也是一个好方法。请记住,根据 IMAP 协议,即使您没有明确请求,服务器也可能会向您发送响应。
In general it's recommended that IMAP clients only have at most one connection to the server at all times. Not only does additional connections require valuable resources on the server but more important the IMAP specification does not guarantee that two connections can select the same mailbox at the same time. Relying on this capability being present on the server may render your client incompatible with those servers.
Instead you should use the protocol as efficient as possible. Note that many commands can operate on a set or range of UIDs. This allows you to make one singe request where you specify every UID instead of making one request for each UID separately.
Another good practice is to not request more data than what is currently needed. For example say that you have a list of messages. Then don't request detailed information for all of them, only request information for the messages that are currently visible.
I highly recommend that you read RFC 2683, IMAP4 Implementation Recommendations. It covers this among other things.
If you decide to use multiple connections anyway then a good approach is usually to use asynchronous operations and not use individual threads explicitly. Combination with some kind of run loop integration is often useful as well, that way your code is called when there is data to read instead of you code having to poll or explicitly check for it. This is often a good approach even if you're only using a single connection. Keep in mind that according to the IMAP protocol the server may send you responses even when you have not explicitly asked for them.