FileDocCategorySizeDatePackage
UseHeaderRecipients.javaAPI DocApache James 2.3.15826Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.mailets

UseHeaderRecipients

public class UseHeaderRecipients extends org.apache.mailet.GenericMailet

Mailet designed to process the recipients from the mail headers rather than the recipients specified in the SMTP message header. This can be useful if your mail is redirected on-route by a mail server that substitutes a fixed recipient address for the original.

To use this, match against the redirection address using the RecipientIs matcher and set the mailet 'class' to UseHeaderRecipients. This will cause the email to be re-injected into the root process with the recipient substituted by all the recipients in the Mail-For, To and Cc headers of the message.

e.g.



version
1.0.0, 24/11/2000

Fields Summary
private boolean
isDebug
Controls certain log messages
Constructors Summary
Methods Summary
private java.util.CollectiongetHeaderMailAddresses(javax.mail.internet.MimeMessage message, java.lang.String name)
Work through all the headers of the email with a matching name and extract all the mail addresses as a collection of addresses.

param
mail the mail message to read
param
name the header name as a String
return
the collection of MailAddress objects.


        if (isDebug) {
            StringBuffer logBuffer =
                new StringBuffer(64)
                        .append("Checking ")
                        .append(name)
                        .append(" headers");
            log(logBuffer.toString());
        }
        Collection addresses = new Vector();
        String[] headers = message.getHeader(name);
        String addressString;
        InternetAddress iAddress;
        if (headers != null) {
            for(int i = 0; i < headers.length; i++) {
                StringTokenizer st = new StringTokenizer(headers[i], ",", false);
                while (st.hasMoreTokens()) {
                    addressString = st.nextToken();
                    iAddress = new InternetAddress(addressString);
                    if (isDebug) {
                        log("Address = " + iAddress.toString());
                    }
                    addresses.add(new MailAddress(iAddress));
                }
            }
        }
        return addresses;
    
public java.lang.StringgetMailetInfo()
Return a string describing this mailet.

return
a string describing this mailet

        return "UseHeaderRecipients Mailet";
    
public voidinit()
Initialize the mailet initializes the DEBUG flag


                
       
        isDebug = (getInitParameter("debug") == null) ? false : new Boolean(getInitParameter("debug")).booleanValue();
    
public voidservice(org.apache.mailet.Mail mail)
Process an incoming email, removing the currently identified recipients and replacing them with the recipients indicated in the Mail-For, To and Cc headers of the actual email.

param
mail incoming email

        MimeMessage message = mail.getMessage();

        // Utilise features of Set Collections such that they automatically
        // ensure that no two entries are equal using the equality method
        // of the element objects.  MailAddress objects test equality based
        // on equivalent but not necessarily visually identical addresses.
        Collection recipients = mail.getRecipients();
        // Wipe all the exist recipients
        recipients.clear();
        recipients.addAll(getHeaderMailAddresses(message, "Mail-For"));
        if (recipients.isEmpty()) {
            recipients.addAll(getHeaderMailAddresses(message, "To"));
            recipients.addAll(getHeaderMailAddresses(message, "Cc"));
        }
        if (isDebug) {
            log("All recipients = " + recipients.toString());
            log("Reprocessing mail using recipients in message headers");
        }

        // Return email to the "root" process.
        getMailetContext().sendMail(mail.getSender(), mail.getRecipients(), mail.getMessage());
        mail.setState(Mail.GHOST);