如何在一个会话中发送多封电子邮件?

发布于 2024-10-06 03:59:19 字数 652 浏览 7 评论 0原文

我想要向不同的收件人发送数千封不同的电子邮件,并且想要打开与我的 SMTP 的连接并保留它。我希望这比重新打开 ervy 邮件的连接更快。我想使用 Apache Commons Email,但如果需要的话可以回退到 Java Mail API。

现在我正在这样做,每次打开连接都会关闭连接:

HtmlEmail email = new HtmlEmail();
email.setHostName(server.getHostName());
email.setSmtpPort(server.getPort());
email.setAuthenticator(new DefaultAuthenticator(server.getUsername(), server.getPassword()));
email.setTLS(true);
email.setFrom("[email protected]");
email.addTo(to);
email.setSubject(subject);
email.setHtmlMsg(htmlMsg);
email.send();

I want to send thousands of different emails to different recipients and would like to open the connection to my SMTP and hold it. I hope this is faster then reopen the connection for ervy mail. I would like to use Apache Commons Email for that, but could fall back to the Java Mail API if necessary.

Right now I'am doing this, what opens a closes the connection every time:

HtmlEmail email = new HtmlEmail();
email.setHostName(server.getHostName());
email.setSmtpPort(server.getPort());
email.setAuthenticator(new DefaultAuthenticator(server.getUsername(), server.getPassword()));
email.setTLS(true);
email.setFrom("[email protected]");
email.addTo(to);
email.setSubject(subject);
email.setHtmlMsg(htmlMsg);
email.send();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2024-10-13 03:59:19

这是我的性能测试课。使用一个连接发送邮件的速度比每次重新打开连接的速度快 4 倍(当您使用公共邮件时会发生什么)。通过使用多线程可以进一步提高性能。

    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", "" + port);

    Session session = Session.getInstance(properties);
    Transport transport = session.getTransport("smtp");

    transport.connect(server, username, password);

    for (int i = 0; i < count; i++) {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        message.setRecipients(Message.RecipientType.TO, address);

        message.setSubject(subject + "JavaMail API");
        message.setSentDate(new Date());

        setHTMLContent(message);
        message.saveChanges();
        transport.sendMessage(message, address);

    }

    transport.close();

Here is my performance test class. Sending the mails using one connection is 4 times faster then reopen the connection every time (what happens when you use commons mail). The performance can be pushed further by using multiple threads.

    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", "" + port);

    Session session = Session.getInstance(properties);
    Transport transport = session.getTransport("smtp");

    transport.connect(server, username, password);

    for (int i = 0; i < count; i++) {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        message.setRecipients(Message.RecipientType.TO, address);

        message.setSubject(subject + "JavaMail API");
        message.setSentDate(new Date());

        setHTMLContent(message);
        message.saveChanges();
        transport.sendMessage(message, address);

    }

    transport.close();
我为君王 2024-10-13 03:59:19

您可以使用之前的代码,但添加以下代码来获取底层会话

email.getMailSession();

您可以通过以下方式添加额外的 java 邮件属性

email.getMailSession().getProperties().put(<key>, <value>);

You can use your earlier code but add the following to get the underlying Session

email.getMailSession();

You can add extra java mail properties by

email.getMailSession().getProperties().put(<key>, <value>);
本宫微胖 2024-10-13 03:59:19

看看 http://java .sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html。有一个示例显示如何发送电子邮件。在调用 Transport 上的 close() 之前,您应该能够发送更多内容。

Have a look at http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html. There is an example showing how to send an email. You should be able to send more before calling close() on the Transport.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文