1

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());
        }
    }
Garbage
  • 1,371
  • 9
  • 21
Vikas
  • 41
  • 1
  • 2
  • 7
  • http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap – Caffeinated May 04 '12 at 05:44

3 Answers3

1

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.

Ved
  • 7,667
  • 6
  • 33
  • 68
  • It is giving OutOfMemoryError exception when I am increasing number of recipients only. I tried to send lot of information in the body part of the email,it is working. – Vikas May 04 '12 at 05:53
  • I can predict out that. Paste the code of sending mail here if possible.You must need to optimize it in some way. – Ved May 04 '12 at 05:55
  • I think you are looping for number of recipients for the mail.So each time process of sending that mail is repeating. As I said earlier, You may send mail to all the recipients in one go by creating the group. – Ved May 04 '12 at 08:21
  • I can't create group, recipients number can change – Vikas May 04 '12 at 11:08
1

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

more help

Subhrajyoti Majumder
  • 38,572
  • 11
  • 72
  • 100
1

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.

See: Java Tuning White Paper

Additionally, if you can post your code here, members can help you improve your code design and possibly find problems (if any).

Garbage
  • 1,371
  • 9
  • 21