我在向大量收件人发送电子邮件时收到 java.lang.OutOfMemoryError: Java heap space。我尝试让 150 个收件人正常工作,但 200 个收件人不工作。
public static void sendMessage(String subject, String text, String sender, String to, String cc, byte[] data, String type) throws Exception {
try {
String EmailServer = "";
String EmailPort = "";
Properties props = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream in = in = classLoader.getResourceAsStream("/resources/config.properties");
props.load(in);
if (in != null)
in.close();
EmailServer = props.getProperty("SMTP_SERVER");
System.out.println("EmailServer================:" + EmailServer);
Properties prop = new Properties();
System.out.println("Properties================:" + prop);
prop.put("mail.smtp.host", EmailServer);
// set the port
//prop.put("mail.smtp.port", EmailPort);
Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setReplyTo(InternetAddress.parse(sender));
msg.setFrom(new InternetAddress(sender));
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(
cc, false));
msg.setContent(text, "text/plain");
msg.setSentDate(new Date());
System.out.println("Before Transport.send================:" + new Date());
Transport.send(msg);
System.out.println("After Transport.send================:" + new Date());
} catch (javax.mail.SendFailedException e) {
e.printStackTrace(System.out);
String invalid = "";
Address[] address = e.getInvalidAddresses();
for (int i = 0; i & lt; address.length ;
i++)
{
if (i == 0)
invalid = invalid + address[i];
else
invalid = invalid + "," + address[i];
}
throw new InvalidEmailAddressException(e.getMessage() + ":" + invalid);
} catch (javax.mail.MessagingException e) {
e.printStackTrace(System.out);
throw new Exception(e.getMessage());
}
}