6

我已经编写了一种批量发送电子邮件的方法,但它非常慢(每 10 秒大约 3 封邮件)。我想发送数千封邮件。有没有办法更快地做到这一点?

我现在使用 gmail 但仅用于测试,最后我想使用自己的 SMTP 服务器发送。

这是代码:

public boolean sendMessages()
{
    try 
    {
        Session session = Session.getInstance(this._properties, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication("user", "password");
            }
        });
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(this.getFrom()));


        message.setSubject(this.getSubject());
        message.setText(this.getBody());                
        for (int i = 0, c = this._addresses.size(); i < c; i++)
        {
            message.setRecipient(Message.RecipientType.TO,  new InternetAddress(this._addresses.get(i)));                    
            Transport.send(message);
        }
        return true;
     } 
     catch(AuthenticationFailedException e) {
         e.printStackTrace();
           return false;
     }
     catch(MessagingException e) {
         e.printStackTrace();
           return false;
     }
}
4

1 回答 1

7

好的,谢谢你的建议。

我的解决方案是:

Transport transport = session.getTransport("smtp");
transport.connect(this._properties.getProperty("mail.smtp.host"), 
Integer.parseInt(this._properties.getProperty("mail.smtp.port")),
    this._properties.getProperty("mail.smtp.user"),
    this._properties.getProperty("mail.smtp.password"));

Address[] addr = new Address[this._addresses.size()];
for (int i = 0, c = this._addresses.size(); i < c; i++)
{
    addr[i] = new InternetAddress(this._addresses.get(i));
}

transport.sendMessage(message, addr);
于 2012-09-24T18:27:45.113 回答