Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javamail,Transport.send()非常慢_Java_Jakarta Mail_Bulk_Mass Emails - Fatal编程技术网

Javamail,Transport.send()非常慢

Javamail,Transport.send()非常慢,java,jakarta-mail,bulk,mass-emails,Java,Jakarta Mail,Bulk,Mass Emails,我写了一个批量发送电子邮件的方法,但速度非常慢(大约每10秒发送3封邮件)。我想发几千封邮件。有没有办法更快地完成这项工作 我现在使用gmail,但只是为了测试,最后我想用我自己的SMTP服务器发送 代码如下: public boolean sendMessages() { try { Session session = Session.getInstance(this._properties, new javax.mail.Authenticator() {

我写了一个批量发送电子邮件的方法,但速度非常慢(大约每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;
     }
}
public boolean sendMessages()
{
尝试
{
Session Session=Session.getInstance(this.\u属性,new javax.mail.Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(“用户”、“密码”);
}
});
MimeMessage message=新MimeMessage(会话);
message.setFrom(新的InternetAddress(this.getFrom());
message.setSubject(this.getSubject());
message.setText(this.getBody());
对于(int i=0,c=this._addresses.size();i
好的,谢谢你的建议

我的解决办法是:

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);
Transport-Transport=session.getTransport(“smtp”);
transport.connect(this._properties.getProperty(“mail.smtp.host”),
整数.parseInt(this.\u properties.getProperty(“mail.smtp.port”),
此._properties.getProperty(“mail.smtp.user”),
这个.u properties.getProperty(“mail.smtp.password”);
Address[]addr=新地址[this.\u addresses.size()];
对于(int i=0,c=this._addresses.size();i
我希望限制是服务器接受电子邮件的速度。您应该期望自己的邮件服务器每秒处理超过10次。您可以通过将Transport.send调用替换为:message.saveChanges()来测量JavaMail创建邮件的速度;message.writeTo(新的BufferedOutputStream(新的FileOutputStream(“msg.txt”));如果发送到服务器的速度低于此速度,则很可能是由于网络性能、协议开销或服务器速度。这将使你的基准偏离。