Reputation: 51
I am getting java.lang.OutOfMemoryError: Java heap space while sending email to large number of recipients. I tried with 150 recipients working fine but for 200 recipients not working.
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());
}
}
Upvotes: 1
Views: 1966
Reputation: 1
It's too late, though I answer this question as I've encountered the same issue with you.
I wasn't able to change the heap size as it was client's server so I wasn't allowed to change the settings.
Instead, I used a trick. Suggest that I have a thousand recipients. Instead of sending emails to 1000 recipients at once, I can send emails 10 times - each time I send emails to 100 recipients.
for(int idx = 0; idx < emailList.size() ; idx++){
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailList.get(idx))); //set recipient
if(( idx != 0 && idx % 100 == 0) || idx == emailList.size() - 1) {
/* this works when the remainder of idx is 0(when idx is multiple of 100)
or when idx is the largest one in this loop*/
Transport.send(msg); //send emails
msg.setRecipients(Message.RecipientType.TO, new Address[]{}); //clear recipients so that the recipients set in message object changes to 0
}
}
I hope this answer might be helpful for anyone encountering this kind of issue.
Upvotes: 0
Reputation: 1520
Most of the times, OutOfMemory errors can be solved by increasing heap size. But I would recommend that you do a performance analysis of your application, check if there are memory leaks or if you can (re)use some variables or memory.
Additionally, if you can post your code here, members can help you improve your code design and possibly find problems (if any).
Upvotes: 1
Reputation: 41200
you increase heap size using -
java -Xms64m -Xmx256m HelloWorld
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
Upvotes: 2
Reputation: 8757
In stead of sending mail to individual recipients, you should create a group for them first. About the error you are getting, it seems like, you should allocate more memory to JVM(However that completely depends upon your requirements, project etc.., hence can't be the best solution).
On later on stage, you may consider to optimize the code to prevent memory related issues.
Upvotes: 1