Apache Commons电子邮件和重新使用SMTP连接 [英] Apache Commons Email and Reusing SMTP Connections

查看:189
本文介绍了Apache Commons电子邮件和重新使用SMTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I am using Commons Email to send email messages, but I have not been able to find a way to share smtp connections between emails sent. I have code like the following:

    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可以使事情快三倍。

Which is very readable, but is slow when I do a large amount of messages, which I believe is the overhead of reconnecting for each message. So I profiled it with the following code and have found that using the reusing the Transport makes things about three times faster.

    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,但是性能却很痛苦。

So I was wondering if there was a way to make Commons Email reuse an SMTP connection for multiple email sendings? I like the Commons Email API better, but the performance is kind of painful.

谢谢,
Ransom

Thanks, Ransom

推荐答案

在挖掘公共源后,我想出了以下解决方案。这应该是有效的,但可能有更好的解决方案我不知道

I came up with the following solution after digging into the commons source itself. This should work, but there may be better solutions I do not know of

    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电子邮件和重新使用SMTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆