views:

84

answers:

1

Hello,

I am currently doing a summer job as Java programmer. We have an application where people can enter their tasks, agenda, etc. The program is a client-server program, so all data is stored on a server.

My boss asked me to make a mail notification system. For example, when a deadline of a task is near, it sends an email to the person assigned to that task.

I implemented this system in the server (which runs 24/24) using JavaMail and it works very well. But after a while (not sure how long) JavaMail stops sending mails. This is the exception I get:

...
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
...
[Mailer] flushing mail queue (10 mails)
[Mailer] exception
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at Server.Mailer.send(Mailer.java:119)
    at Server.Mailer.flush(Mailer.java:84)
    at Server.Mailer.run(Mailer.java:103)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1297)
    ... 8 more

When running longer than X hours, it keeps giving these exceptions. So I thought it was because the connection with the SMTP server timed-out. So I changed this code:

Session session = Session.getDefaultInstance(properties, authenticator);

to:

Session session = Session.getInstance(properties, authenticator);

so it would create a new session every time. I thought this would force JavaMail to reconnect to the SMTP server and then the problem would be solved. But that didn't solve it, I still get these exceptions...

Does anyone know how to fix this?

PS: This is the code of my send function

Session session = Session.getInstance(properties, authenticator);
MimeMessage message = new MimeMessage(session);

message.setSubject(mail.getSubject());
message.setContent(mail.getHTML().toString(), "text/html");
message.setFrom(mail.getSender());
message.setRecipients(javax.mail.Message.RecipientType.TO, mail.getRecipients());

Transport.send(message);
A: 

I'd start with catching SMTPAddressFailedException exceptions

try {
    Transport.send(message);
} catch (SMTPAddressFailedException e) {
    throw new SendFailedException("Unable to send to " + mail.getRecipients(), e);
}

It could be as simple as one of your users having a typo in their email configuration, if thats the case what you should probably do is set a flag on the relevent account which will promt the user next time they login to your application to verify their email settings.

Jon Freedman
Well, I was just thinking about that too. Would it also help if I would send the mail for each email address? So instead of "message.setRecipients(javax.mail.Message.RecipientType.TO, mail.getRecipients());" I would do "for(InternetAddress recipient: mail.getRecipients()) { ... message.setRecipient(javax.mail.Message.RecipientType.TO, recipient); ... }
gillis
Rather than sending multiple messages you should set `mail.smtp.sendpartial` to true, see http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
Jon Freedman
Thanks! That's exactly what I need!
gillis