2

I tried to deploy my first sample mailet to apache james v2.3.2 but it gets into an endless loop, sending me emails again and again even after restarting the server. The only way to stop it was to clean the spool folder.

In the config.xml I configured my mailet as follows:

<mailet match="All" class="MyMailet" onMailetException="ignore">
  <supportMailAddress>support@[mydomain].com</supportMailAddress>
</mailet>

All my mailet does is email from support@[mydomain].com to a me@[mydomain].com mailbox:

public class MyMailet extends GenericMailet {
  private MailAddress supportMailAddress;

  public void init() throws ParseException {
    supportMailAddress = new MailAddress(getInitParameter("supportMailAddress"));
  }

  public void service(Mail mail) throws MessagingException {
    MailAddress sender = mail.getSender();
    MailAddress realMailbox = new MailAddress("me@[mydomain].com");

    try {
      sendToRealMailbox(mail, realMailbox);
    } catch (MessagingException me) {
      me.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }

    mail.setState(Mail.GHOST);
  }


  private void sendToRealMailbox(Mail mail, MailAddress realMailbox) throws MessagingException, IOException {
    Properties props = System.getProperties();
    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    message.setFrom(supportMailAddress.toInternetAddress());
    message.setRecipient(Message.RecipientType.TO, realMailbox.toInternetAddress());
    message.setSubject("MyMailet: " + mail.getMessage().getSubject());
    message.setText("MyMailet: message body");
    message.setSentDate(new Date());

    Collection recipients = new Vector();
    recipients.add(realMailbox);
    getMailetContext().sendMail(supportMailAddress, recipients, message);
  }

  public String getMailetInfo() {
    return "MyMailet";
  }
}

What am I doing wrong?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

1 Answers1

2

You used the matcher "All", i.e.

<mailet match="All">

This tells James to run this mailet for all emails. So when you send the first email to support@[mydomain].com, the mailet fires and sends an email to me@[mydomain].com. The email coming in to me@[mydomain].com causes the mailet to fire again and send another email to me@[mydomain].com, which causes the mailet to fire again, etc...

Try using the "RecipientIs" matcher instead, i.e.

<mailet match="RecipientIs=support@[mydomain].com">

This will fire the mailet only when the email comes to support@[mydomain].com.

See this list of James matchers with examples.

Bad Tea
  • 41
  • 4