Hi Eric,

I am writing a custom mailet and based on the condition, I might have
to deny sending mail message and bounce it.

My bounce method is getting called, but unable to bounce the mail message.

I have written a helper class and implemented my bounce method.

Following is the code.

within my mailet I am calling the method bounce

mailetcntxtwrapper = new MailetContextWrapper();
mailetcntxtwrapper.bounce(mail, "Denied access from this message", new
MailAddress(username));


public class MailetContextWrapper implements MailetContext  {

    @Override
    public void bounce(Mail mail, String message, MailAddress bouncer)
throws MessagingException {
        if(mail == null) {
                System.out.println("Mail is null");
        }

        if (mail.getSender() == null) {
            if (log.isInfoEnabled())
                log.info("Mail to be bounced contains a null (<>)
reverse path.  No bounce will be sent.");
            return;
        } else {
            // Bounce message goes to the reverse path, not to the Reply-To
            // address
            if (log.isInfoEnabled())
                log.info("Processing a bounce request for a message
with a reverse path of " + mail.getSender().toString());
        }

        MailImpl reply = rawBounce(mail, message);
        // Change the sender...
        reply.getMessage().setFrom(bouncer.toInternetAddress());
        reply.getMessage().saveChanges();
        // Send it off ... with null reverse-path
        reply.setSender(null);
        sendMail(reply);
        LifecycleUtil.dispose(reply);
    }
        
    /**
     * Generates a bounce mail that is a bounce of the original message.
     *
     * @param bounceText the text to be prepended to the message to
describe the bounce
     *                   condition
     * @return the bounce mail
     * @throws MessagingException if the bounce mail could not be created
     */
    private MailImpl rawBounce(Mail mail, String bounceText) throws
MessagingException {
        System.out.println("rawbound method called");
        // This sends a message to the james component that is a bounce of the
        // sent message
        MimeMessage original = mail.getMessage();
        MimeMessage reply = (MimeMessage) original.reply(false);
        reply.setSubject("Re: " + original.getSubject());
        reply.setSentDate(new Date());
        Collection<MailAddress> recipients = new HashSet<MailAddress>();
        recipients.add(mail.getSender());
        InternetAddress addr[] = {new
InternetAddress(mail.getSender().toString())};
        reply.setRecipients(Message.RecipientType.TO, addr);
        reply.setFrom(new
InternetAddress(mail.getRecipients().iterator().next().toString()));
        reply.setText(bounceText);
        reply.setHeader(RFC2822Headers.MESSAGE_ID, "replyTo-" + mail.getName());
        return new MailImpl("replyTo-" + mail.getName(), new
MailAddress(mail.getRecipients().iterator().next().toString()),
recipients, reply);
}

}

Please guide me to get my custom bounce working. Also I want to make
sure that once the bounce happens, the mail is not processed any
further. What configuration should I be looking at within the mailet
configuration.

Appreciate your help :)

Thank you,
Regards,
Rajender

---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to