如何有效地使用 javax.mail API 发送批量邮件?&我们可以使用重用经过身份验证的会话来提高速度吗?

How to Send bulk mails using javax.mail API efficiently? amp; Can we use reuse authenticated sessions to improve speed?(如何有效地使用 javax.mail API 发送批量邮件?amp;我们可以使用重用经过身份验证的会话来提高速度吗?)

本文介绍了如何有效地使用 javax.mail API 发送批量邮件?&我们可以使用重用经过身份验证的会话来提高速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 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.

-纳伦德拉

推荐答案

你是如何发送消息的?JavaMail FAQ 建议静态 Transport.send 方法将为每条消息打开一个新连接,因为它是一种方便的方法,可以创建合适的 Transport 实例,连接它,调用 sendMessage 和然后再次关闭连接.如果您从 Session 获得自己的 Transport 实例,您可以连接一次,然后重复调用 sendMessage 以在一个连接上发送多条消息,然后最后 close 它.类似于(未经测试):

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();
}

更新为使用资源块的尝试:

Updated to use try with resources block:

try (Transport t = session.getTransport()) {
    t.connect();
    for(Message m : messages) {
        m.saveChanges();
        t.sendMessage(m, m.getAllRecipients());
    }
}

这篇关于如何有效地使用 javax.mail API 发送批量邮件?&我们可以使用重用经过身份验证的会话来提高速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何有效地使用 javax.mail API 发送批量邮件?&我们可以使用重用经过身份验证的会话来提高速度吗?

基础教程推荐