1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.mailet.GenericMailet; 21 import org.apache.mailet.Mail; 22 import org.apache.mailet.MailAddress; 23 24 import javax.mail.MessagingException ; 25 import javax.mail.internet.MimeMessage ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.Vector ; 29 30 35 public class LocalDelivery extends GenericMailet { 36 43 public void service(Mail mail) throws MessagingException { 44 Collection recipients = mail.getRecipients(); 45 Collection errors = new Vector (); 46 MimeMessage message = mail.getMessage(); 47 for (Iterator i = recipients.iterator(); i.hasNext(); ) { 48 MailAddress recipient = (MailAddress) i.next(); 49 try { 50 MimeMessage localMessage = new MimeMessage (message) { 52 protected void updateHeaders() throws MessagingException { 53 if (getMessageID() == null) super.updateHeaders(); 54 else { 55 modified = false; 56 } 57 } 58 }; 59 localMessage.addHeader("Delivered-To", recipient.toString()); 60 localMessage.saveChanges(); 61 62 getMailetContext().storeMail(mail.getSender(), recipient, localMessage); 63 } catch (Exception ex) { 64 getMailetContext().log("Error while storing mail.", ex); 65 errors.add(recipient); 66 } 67 } 68 69 if (!errors.isEmpty()) { 70 getMailetContext().sendMail(mail.getSender(), 76 errors, message, Mail.ERROR); 77 } 78 mail.setState(Mail.GHOST); 80 } 81 82 87 public String getMailetInfo() { 88 return "Local Delivery Mailet"; 89 } 90 } 91 | Popular Tags |