我是否可以在没有连接和断开邮件服务的情况下应用此循环?这个循环对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");
答案 0 :(得分: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);
答案 1 :(得分: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);
}