如何有效地使用javax.mail API发送批量邮件? &安培;我们可以使用重用身份验证会话来提高速度吗? [英] How to Send bulk mails using javax.mail API efficiently? & Can we use reuse authenticated sessions to improve speed?
问题描述
我可以使用javax.mail API发送邮件。但是这个问题在平均每个邮件发送到目的地大约需要4.3秒。
I am able to send a mail using javax.mail API. But the problem here is on an average for each mail it taking around 4.3 seconds to send to destination.
如果我按顺序发送20封邮件,大概需要86.599秒。对于我的要求,这种方法将不起作用。我正在寻找一种可以在较短时间内发送大量邮件的方法。
If I am sending a 20 mails sequentially, it takes around 86.599 seconds. For my requirement this approach will not work. I am looking for an approach which can send large number of mails in less time.
当我查看调试日志时,API正在尝试向SMTP服务器进行身份验证它发送的每个消息。但是,我正在创建一个会话只有一次,并使用相同的会话为我发送的所有邮件。现在我的问题是每次向smtp服务器认证自身时都不是一个开销的过程。不是有更好的方法?
When I looked at the debug log, the API is trying to authenticate to SMTP server for each and every message it sending. But I am creating a session only one time and using the same session for all the mails I am sending. Now my question is Isn't it a overhead process every time authenticating itself to the smtp server. Isn't there a better approach ?
以下是您可能会发现有帮助的日志跟踪。
Below is the log trace you may find helpful.
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
请让我知道您对此的想法,并对此有任何帮助,非常感谢。
Please let me know your thoughts on this and any help on this is really appreciated.
-Narendra
-Narendra
推荐答案
你如何发送消息? JavaMail常见问题解答表明静态 Transport.send
方法将为每个消息打开一个新的连接,因为它是创建一个合适的传输
实例的方便方法,连接它,调用 sendMessage
然后再次关闭连接。如果您从会话
中获取您自己的传输
实例,则可以连接一次,然后调用 sendMessage
反复在一个连接上发送几个消息,最后关闭
。一些沿着(未经测试)的行:
How are you sending the messages? The JavaMail FAQ suggests that the static Transport.send
method will open a fresh connection for each message, as it is a convenience method that creates a suitable Transport
instance, connects it, calls sendMessage
and then closes the connection again. If you get your own Transport
instance from the Session
you can connect once, then call sendMessage
repeatedly to send several messages on the one connection, and finally close
it. Something along the lines of (untested):
Transport t = session.getTransport();
t.connect();
try {
for(Message m : messages) {
m.saveChanges();
t.sendMessage(m, m.getAllRecipients());
}
} finally {
t.close();
}
这篇关于如何有效地使用javax.mail API发送批量邮件? &安培;我们可以使用重用身份验证会话来提高速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!