简体   繁体   English

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

[英]How to send multiple emails in one session?

I want to send thousands of different emails to different recipients and would like to open the connection to my SMTP and hold it. 我想向不同的收件人发送数千封不同的电子邮件,并希望打开与我的SMTP的连接并保留它。 I hope this is faster then reopen the connection for ervy mail. 我希望这更快,然后重新打开ervy邮件的连接。 I would like to use Apache Commons Email for that, but could fall back to the Java Mail API if necessary. 我想使用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("test@example.com");
email.addTo(to);
email.setSubject(subject);
email.setHtmlMsg(htmlMsg);
email.send();

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). 使用一个连接发送邮件的速度要快4倍,然后每次重新打开连接(当您使用公共邮件时会发生什么)。 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();

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 您可以添加额外的Java邮件属性

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

Have a look at http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html . 请查看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. 您应该能够在传输上调用close()之前发送更多内容。

no need xtra code at all, just put your all email recipients and separate with comma. 根本不需要xtra代码,只需将您的所有电子邮件收件人与逗号分开即可。

MimeMessage pesan = new MimeMessage(session); pesan.setFrom(new InternetAddress("email_from@host.com")); pesan.setRecipients(Message.RecipientType.TO, InternetAddress.parseHeader("first_email@host.com,second_email@host.com,dst_email@host.com",false));

and do the same thing for Message.RecipientType.CC and Message.RecipientType.BCC if there is more than 1 email recipients hope its help :).. 并为Message.RecipientType.CCMessage.RecipientType.BCC做同样的事情,如果有超过1个电子邮件收件人希望它的帮助:) ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM