【问题标题】:Apache Commons Email and Reusing SMTP ConnectionsApache Commons 电子邮件和重用 SMTP 连接
【发布时间】:2011-10-05 09:59:40
【问题描述】:

目前我正在使用Commons Email 发送电子邮件,但我一直无法找到在发送的电子邮件之间共享 smtp 连接的方法。我有如下代码:

    Email email = new SimpleEmail();
    email.setFrom("example@example.com");
    email.addTo("example@example.com");
    email.setSubject("Hello Example");
    email.setMsg("Hello Example");
    email.setSmtpPort(25);
    email.setHostName("localhost");
    email.send();

这非常易读,但是当我处理大量消息时速度很慢,我相信这是为每条消息重新连接的开销。所以我用下面的代码对其进行了分析,发现使用重用 Transport 可以使事情快三倍。

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport = mailSession.getTransport("smtp");
    transport.connect("localhost", 25, null, null);

    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress("example@example.com"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("example@example.com"));
    message.setSubject("Hello Example");
    message.setContent("Hello Example", "text/html; charset=ISO-8859-1");

    transport.sendMessage(message, message.getAllRecipients());

所以我想知道是否有办法让 Commons Email 重用一个 SMTP 连接来发送多个电子邮件?我更喜欢 Commons Email API,但性能有点痛苦。

谢谢, 赎金

【问题讨论】:

    标签: java email apache-commons-email


    【解决方案1】:

    在深入研究公共资源本身后,我提出了以下解决方案。这应该可行,但可能有更好的解决方案我不知道

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        Session mailSession = Session.getDefaultInstance(props, null);
        Transport transport = mailSession.getTransport("smtp");
        transport.connect("localhost", 25, null, null);
    
        Email email = new SimpleEmail();
        email.setFrom("example@example.com");
        email.addTo("example@example.com");
        email.setSubject("Hello Example");
        email.setMsg("Hello Example");
        email.setHostName("localhost"); // buildMimeMessage call below freaks out without this
    
        // dug into the internals of commons email
        // basically send() is buildMimeMessage() + Transport.send(message)
        // so rather than using Transport, reuse the one that I already have
        email.buildMimeMessage();
        Message m = email.getMimeMessage();
        transport.sendMessage(m, m.getAllRecipients());
    

    【讨论】:

    • 您对 apache commons mail 的性能满意吗?在 Spring email 和 Apache commons mail 之间,我应该更喜欢哪一个来发送繁重的附件和更快的性能?
    【解决方案2】:

    我们不能通过使用 getMailSession() 从第一封电子邮件中获取邮件会话并使用 setMailSession() 将其放入所有后续邮件中来更轻松地实现这一点吗?

    不是 100% 确定是什么

    请注意,传递用户名和密码(在邮件身份验证的情况下)将使用 DefaultAuthenticator 创建一个新的邮件会话。这是一种方便,但可能会出乎意料。如果使用邮件身份验证但未提供用户名和密码,则实现假定您已设置身份验证器并将使用现有邮件会话(如预期的那样)。

    来自 javadoc 的意思是:-/ http://commons.apache.org/email/api-release/org/apache/commons/mail/Email.html#setMailSession%28javax.mail.Session%29

    另见: https://issues.apache.org/jira/browse/EMAIL-96

    不知道如何在此处继续...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2014-11-29
      • 2018-09-22
      • 2010-10-25
      • 2011-11-29
      • 2018-11-17
      • 1970-01-01
      相关资源
      最近更新 更多