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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4334753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:03:32  来源:igfitidea点击:

How to send multiple emails in one session?

javaemailapache-commons-email

提问by Tim Büthe

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.

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

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

采纳答案by Tim Büthe

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.

这是我的性能测试课。使用一个连接发送邮件比每次都重新打开连接快 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();

回答by adrianboimvaser

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.

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

回答by hsampson

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

您可以使用之前的代码,但添加以下内容以获取底层 Session

email.getMailSession();

You can add extra java mail properties by

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

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

回答by Mang Jojot

no need xtra code at all, just put your all email recipients and separate with comma.

根本不需要额外的代码,只需将所有电子邮件收件人放在一起并用逗号分隔。

MimeMessage pesan = new MimeMessage(session); pesan.setFrom(new InternetAddress("[email protected]")); pesan.setRecipients(Message.RecipientType.TO, InternetAddress.parseHeader("[email protected],[email protected],[email protected]",false));

MimeMessage pesan = new MimeMessage(session); pesan.setFrom(new InternetAddress("[email protected]")); pesan.setRecipients(Message.RecipientType.TO, InternetAddress.parseHeader("[email protected],[email protected],[email protected]",false));

and do the same thing for Message.RecipientType.CCand Message.RecipientType.BCCif there is more than 1 email recipients hope its help :)..

做同样的事情 Message.RecipientType.CC并且Message.RecipientType.BCC如果有超过1个电子邮件收件人希望它帮助:) ..