UseHeaderRecipientspublic 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.
|
Fields Summary |
---|
private boolean | isDebugControls certain log messages |
Methods Summary |
---|
private java.util.Collection | getHeaderMailAddresses(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.
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.String | getMailetInfo()Return a string describing this mailet.
return "UseHeaderRecipients Mailet";
| public void | init()Initialize the mailet
initializes the DEBUG flag
isDebug = (getInitParameter("debug") == null) ? false : new Boolean(getInitParameter("debug")).booleanValue();
| public void | service(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.
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);
|
|