简体   繁体   English

是否可以调用TidSMTP.Connect一次,然后从线程调用TidSMTP.Send?

[英]Is it possible to call TidSMTP.Connect once and then from threads call TidSMTP.Send?

I am writing an app that sends e-mail messages using Indy. 我正在编写一个使用Indy发送电子邮件的应用程序。

Every message is sent by a thread. 每个消息都是由线程发送的。

Currently I am connecting to TidSMTP inside the thread, so for sending 10 mails, I need 10 threads and I connect 10 times. 当前,我正在连接到TidSMTP内部的TidSMTP ,因此要发送10个邮件,我需要10个线程,并且连接10次。

Is it safe (which are the drawbacks?) of having a single TidSMTP (outside of the thread), call Connect once and then call TidSMTP.Send inside the thread? 拥有单个TidSMTP (在线程外部),调用一次Connect ,然后再调用TidSMTP.Send在线程内部发送是否安全(缺点?)?

Will TidSMTP manage thing correctly? TidSMTPTidSMTP管理事情吗?

Note: the idea is to avoid to connect every time (if possible), in case of many emails to be sent it could be an advantage. 注意:这样做的目的是避免每次都连接(如果可能的话),如果要发送许多电子邮件,这可能是一个优势。 (does it makes sense to get worried for this, or calling Connect in every thread is pefectly ok?). (为此担心或在每个线程中调用Connect完全可以吗?)。

Why don't you use only 1 thead in which you have a TIdSMTP and a TList in which you store TIdMessage's and after each send you free the TIdMessage from the list, in this case you avoid overhead and keep it simple. 为什么不只使用1个主题,其中有一个TIdSMTP和一个存储TIdMessage的TList,并在每次发送后从列表中释放TIdMessage,在这种情况下,您可以避免开销并保持简单。 What if you want to send 200 e-mails, well if you start 200 threads then your application will use over 200 Mb only for those 200 threads not to mention that there can be problems starting that many threads in your application. 如果您要发送200封电子邮件,那么如果您启动200个线程该怎么办,那么您的应用程序将仅对这200个线程使用200 Mb以上的内存,更不用说在应用程序中启动那么多线程可能会出现问题。 Bottom line add a TList in which you temporarily store prepared TIdMessages and inside the thread a while loop that will check if the list has any messages to send, if it has then grab, send and remove from list. 在最后一行添加一个TList,在其中临时存储准备好的TIdMessages,并在线程内的while循环内,该循环将检查列表是否有要发送的消息,如果要从列表中抓取,发送和删除。

Technically, you can call Connect() in one thread and then call Send() in other threads. 从技术上讲,您可以在一个线程中调用Connect(),然后在其他线程中调用Send()。 However, you would have to serialize access to Send(), otherwise the sending threads can overlap each other and corrupt the SMTP communication. 但是,您必须序列化对Send()的访问,否则,发送线程可能会彼此重叠并破坏SMTP通信。 Dorin's suggestion to move all of the SMTP traffic to a single thread with a queue is the best choice. Dorin建议将所有SMTP通信移至带有队列的单个线程中是最佳选择。 However, the queue itself needs to be accessed in a thread-safe manner, so using a plain TList or TQueue by itself it not good enough. 但是,队列本身需要以线程安全的方式进行访问,因此仅使用普通的TList或TQueue不够好。 Either use TThreadList (or Indy's own TIdThreadSafeList) instead of TList, or wrap the TQueue with a separate TCriticalSection. 使用TThreadList(或Indy自己的TIdThreadSafeList)代替TList,或使用单独的TCriticalSection包装TQueue。

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

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