0

My email code works well when I just send an email to a few people, but when I sending to all users(177) in contact, I got this error:

[ERROR]  - org.apache.commons.mail.EmailException: Sending the email to the following server failed : hlrdwd.com:25

The code is below:

HtmlEmail email = new HtmlEmail();
        email.setCharset("utf-8");
        if (vo.getContent() != null && vo.getContent().trim().length() > 0) {
            email.setHtmlMsg(vo.getContent());
        } else {
            email.setHtmlMsg("   ");
        }
        email.setSubject(vo.getTitle());
        email.setFrom(vo.getSender(), currentuname);
        email.setHostName(Property.getSmtp());
        List<Map<String, String>> toList = mm.formatAddress(vo
                .getReceiver());
        if (toList != null) {
            for (int i = 0; i < toList.size(); i++) {
                Map<String, String> tMap = toList.get(i);
                email.addTo(tMap.get(mm.KEY_EMAIL), tMap.get(mm.KEY_NAME));
                System.out.println(tMap.get(mm.KEY_EMAIL));
            }
        }
        email.setAuthentication(currentuser, password);

        String messageid = email.send();

I google this and add email.setTLS(true);, but still can not work. Waiting your help!

Joshua
  • 125
  • 2
  • 16

1 Answers1

1

The problem is that the receiving mail server doesn't like messages being sent to too many people at the same time. As a reference, postfix by default rejects messages with more than 50 recipients.

The simplest solution is to send multiple messages, rather than sending to everyone at once. In the extreme, you could send a message per user -- then you get the opportunity to customise the messages if you want, which also makes them less likely to be filtered as spam.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • "--- also makes them less likely to be filtered as spam" -- or more likely, or equally likely, as the case may be. There is no "one size fits all" to get past spam filters, or spammers would already have started doing that. – tripleee Aug 01 '12 at 10:26
  • @tripleee: I was specifically thinking about the customisations: I'd suggest that it would be a rare spam engine that would mark a mail down for setting up the To header correctly and putting the recipient's name in the body. – Andrew Aylett Aug 01 '12 at 11:49
  • Not harder for a spammer than for your average PHP newbie here on SO. Don't have statistics here but would guesstimate that 10 to 30 per cent of my spam has my address in the To: header. (Real name is arguably harder for the spammers.) If you have a botnet, individualized spam messages might actually work better than broadly Bcc:ed messages (harder for the zombie's ISP to distinguish from normal legit traffic). – tripleee Aug 01 '12 at 15:13
  • Thank you. Would you mind tell me how to send multiple messages with 180 receivers at one time? If I send 4 messages with each less than 50 users instead, 4 messageid will be returned which is cannot acceptable. – Joshua Aug 02 '12 at 01:35
  • 1
    If multiple messages are not acceptable, this cannot be solved with the current constraints. Maybe move to a different mail provider with a higher limit (but expect them to break it into smaller chunks of messages behind the scenes). – tripleee Aug 02 '12 at 04:37