简体   繁体   English

C# IMAP 客户端 - 与邮件服务器的多个连接

[英]C# IMAP client - multiple connections to mail server

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).在尝试了许多 IMAP API 之后,我决定自己编写(大多数是 memory 猪,有些只是不工作,memory 异常等等等等)。

Anyway I have wrote some code that works (using TCPClient object) and its good so far.无论如何,我已经编写了一些有效的代码(使用 TCPClient 对象)并且到目前为止它很好。 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.例如:假设我得到了所有 UID 的列表,然后我循环遍历这个列表,为每条消息获取我想要的(Body、Header 等)。

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.因此,我可以一次处理 10 个,而不是一次循环一个 UID。

What would be the best approach here?这里最好的方法是什么? An array of TCP Clients, each with its own thread?一组 TCP 客户端,每个都有自己的线程?

Thanks.谢谢。

In general it's recommended that IMAP clients only have at most one connection to the server at all times.一般来说,建议 IMAP 客户端始终最多只有一个与服务器的连接。 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.额外的连接不仅需要服务器上的宝贵资源,更重要的是 IMAP 规范不保证两个连接可以同时使用同一个邮箱。 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.请注意,许多命令可以对一组或一系列 UID 进行操作。 This allows you to make one singe request where you specify every UID instead of making one request for each UID separately.这允许您在指定每个 UID 的地方发出一个单一请求,而不是分别为每个 UID 发出一个请求。

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 .我强烈建议您阅读 RFC 2683, IMAP4 实施建议 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.请记住,根据 IMAP 协议,即使您没有明确要求,服务器也可能会向您发送响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM