Following from the attached email, I've been working on a solution to my redirection 
problem.  I think I've pretty much solved it.  I've tested it for a while and it seems 
to work, so here's there new Mailet which is fairly self-explanatory.

See below...

On Tuesday, November 21, 2000, at 04:34 PM, Ye Tao wrote:

> I might be wrong, but I guess James is already late 
> when it receives the email. The address is set by the 
> unix sendmail (as triggered by .forward), and james is 
> already in the downstream. 
>  
>   
> > I'm now trying to deal with some email which is 
> > coming in having been forwarded by a Unix server 
> > using the standard ".forward" facility. 
> >  
> > I have email coming in being sent to addresses such 
> > as: 
> >  
> >     [EMAIL PROTECTED] 
> >     [EMAIL PROTECTED] 
> >  
> > They are all being .forwarded to 
> > "[EMAIL PROTECTED]" 
> >  
> > When JAMES receives them I would like the original 
> > destination to be used for "matching", but at 
> > present the "[EMAIL PROTECTED]" address is being 
> > used instead. 
> >  
> > Is there any way round this with the current 
> > version? 
> >  
> > If not, which bit of code should I try adapting - 
> > any hints much appreciated. 



/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package org.apache.james.transport.mailets;

import org.apache.james.core.*;
import org.apache.mailet.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * <p>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.</p>
 *
 * <p>To use this, match against the redirection address using the
 * <code>RecipientIs</code> matcher and set the mailet 'class' to
 * <code>UseHeaderRecipients</code>.  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, Cc, Bcc and Newsgroup headers
 * of the message.</p>
 *
 * <p>e.g.</p>
 * <pre>
 *    <mailet match="RecipientIs=forwarded@myhost"
 *            class="UseHeaderRecipients">
 *    </mailet>
 * </pre>
 *
 * @version 1.0.0, 24/11/2000
 * @author Stuart Roebuck <[EMAIL PROTECTED]>
 */
public class UseHeaderRecipients extends GenericMailet {

    /**
     * Process an incoming email, removing the currently identified
     * recipients and replacing them with the recipients indicated in
     * the Mail-For, To, Cc, Bcc & Newsgroup headers of the actual
     * email.
     *
     * @param mail incoming email
     */
    public void service(Mail mail) throws MessagingException {
        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.
        HashSet recipients = new HashSet();
        recipients.addAll(getHeaderMailAddresses(message, "Mail-For"));
        recipients.addAll(getHeaderMailAddresses(message, "To"));
        recipients.addAll(getHeaderMailAddresses(message, "Cc"));
        recipients.addAll(getHeaderMailAddresses(message, "Bcc"));
        recipients.addAll(getHeaderMailAddresses(message, "Newsgroups"));
        // recipients should now be a collection of all the recipients
        // listed in the email headers.
        log("All recipients = " + recipients.toString());
        
        // Replace the current recipient list for the Mail object with
        // the recipients derived from the message headers.
        ( (MailImpl) mail).setRecipients( (Collection) recipients);
        
        log("Reprocessing mail using recipients in message headers");
        // Return email to the "root" process.
        getMailetContext().sendMail(mail.getSender(), mail.getRecipients(), 
mail.getMessage(), "root");
        mail.setState(Mail.GHOST);
    }


    public String getMailetInfo() {
        return "UseHeaderRecipients Mailet";
    }
    
    /**
     * 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.
     */
    private Collection getHeaderMailAddresses(MimeMessage message, String name) throws 
MessagingException {
        log("Checking " + name + " headers");
        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);
                    log("Address = " + iAddress.toString());
                    addresses.add(new MailAddress(iAddress));
                }
            }
        }
        return addresses;
    }

}


Regards,

Stuart.


-------------------------------------------------------------------------
Stuart Roebuck                                  [EMAIL PROTECTED]
Lead Developer                                  Mac OS X, Java, XML, etc.
ADOLOS                                             http://www.adolos.com/

------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives:  <http://www.mail-archive.com/james%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to