0

有什么方法可以在不连接和断开邮件服务的情况下应用此循环?该循环对于 2 或 5 人工作正常,但何时达到 100 人将需要很长时间才能发送所有消息。每个循环大约需要 3 秒。

     while(rs.next()){
     items = rs.getString("email");          



    try {

        Properties mail = new Properties();
        mail.setProperty("mail.smtp.host", "smtp.gmail.com");
        mail.setProperty("mail.smtp.starttls.enable","true");
        mail.setProperty("mail.smtp.port", "25");
        mail.setProperty("mail.smtp.user", "cuatrocuadrado@gmail.com" );
        mail.setProperty("mail.smtp.auth", "true");


         session=Session.getDefaultInstance(mail);
       message= new MimeMessage(session);
         message.setFrom(new InternetAddress("cuatrocuadrado@gmail.com"));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(items));
        //message.addRecipient(Message.RecipientType.BCC, new InternetAddress());
        message.setSubject(Email.tSubject.getText());
        message.setText(Email.areaEmail.getText());

         Transport t = session.getTransport("smtp");
        t.connect("cuatrocuadrado@gmail.com","*********");

        t.sendMessage(message, message.getAllRecipients());

        t.close();

        } catch (MessagingException ex) {
        JOptionPane.showMessageDialog(null,ex.getMessage());
    }
} }  catch (SQLException ex) {
        Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
    }JOptionPane.showMessageDialog(null,"Success");
4

2 回答 2

0

试试这个,但你会再次面临另一个问题......如果你的服务器没有 NS 条目,gmail 将不允许你发送给多个收件人。如果您确实有亚马逊或其他云服务,您可以使用它们,但它们会收费:P

try {

    Properties mail = new Properties();
    mail.setProperty("mail.smtp.host", "smtp.gmail.com");
    mail.setProperty("mail.smtp.starttls.enable","true");
    mail.setProperty("mail.smtp.port", "25");
    mail.setProperty("mail.smtp.user", "cuatrocuadrado@gmail.com" );
    mail.setProperty("mail.smtp.auth", "true");


    session=Session.getDefaultInstance(mail);
    message= new MimeMessage(session);
    message.setFrom(new InternetAddress("cuatrocuadrado@gmail.com"));

    ArrayList<InternetAddress> many = new ArrayList<InternetAddress>();
    while(rs.next()){
        many.add(new InternetAddress(rs.getString("email")));    
    }
    message.addRecipient(Message.RecipientType.TO, many.toArray(new InternetAddress[many.length]));
    //message.addRecipient(Message.RecipientType.BCC, new InternetAddress());
    message.setSubject(Email.tSubject.getText());
    message.setText(Email.areaEmail.getText());

    Transport t = session.getTransport("smtp");
    t.connect("cuatrocuadrado@gmail.com","*********");

    t.sendMessage(message, message.getAllRecipients());

    t.close();

}catch (MessagingException ex) {
    JOptionPane.showMessageDialog(null,ex.getMessage());
}catch (SQLException ex) {
    Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
}
于 2013-01-29T19:25:27.600 回答
0

试试这个可能对你有帮助。

从结果集中获取电子邮件值并将其动态添加到数组中。

String[] to = {"test1@gmail.com","test2@gmail.com","test3@gmail.com","test4@gmail.com"};
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
    addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(RecipientType.TO, addressTo); 
于 2013-01-29T19:17:56.007 回答