1 package org.apache.james.samples.mailets; 2 3 16 17 import java.io.IOException ; 18 import java.util.Collection ; 19 import java.util.Enumeration ; 20 import java.util.Iterator ; 21 22 import javax.mail.Address ; 23 import javax.mail.Flags ; 24 import javax.mail.Header ; 25 import javax.mail.MessagingException ; 26 import javax.mail.Flags.Flag; 27 import javax.mail.internet.InternetAddress ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.internet.NewsAddress ; 30 import javax.mail.internet.MimeMessage.RecipientType; 31 32 import org.apache.mailet.Mail; 33 import org.apache.mailet.MailAddress; 34 import org.apache.mailet.Mailet; 35 import org.apache.mailet.MailetConfig; 36 import org.apache.mailet.MailetContext; 37 38 42 public class InstrumentationMailet implements Mailet { 43 44 private MailetConfig config; 45 46 public void destroy() { 47 48 } 49 50 public String getMailetInfo() { 51 return "Example mailet"; 52 } 53 54 public MailetConfig getMailetConfig() { 55 return config; 56 } 57 58 public void init(MailetConfig config) throws MessagingException { 59 this.config = config; 60 } 61 62 public void service(Mail mail) throws MessagingException { 63 MailetContext context = config.getMailetContext(); 64 context.log("######## MAIL STARTS"); 65 context.log(""); 66 67 MimeMessage message = mail.getMessage(); 68 69 context.log("Mail named: " + mail.getName()); 70 71 for (Iterator it=mail.getAttributeNames(); it.hasNext();) { 72 String attributeName = (String ) it.next(); 73 context.log("Attribute " + attributeName); 74 } 75 context.log("Message size: " + mail.getMessageSize()); 76 context.log("Last updated: " + mail.getLastUpdated()); 77 context.log("Remote Address: " + mail.getRemoteAddr()); 78 context.log("Remote Host: " + mail.getRemoteHost()); 79 context.log("State: " + mail.getState()); 80 context.log("Sender host: " + mail.getSender().getHost()); 81 context.log("Sender user: " + mail.getSender().getUser()); 82 Collection recipients = mail.getRecipients(); 83 for (Iterator it = recipients.iterator(); it.hasNext();) 84 { 85 MailAddress address = (MailAddress) it.next(); 86 context.log("Recipient: " + address.getUser() + "@" + address.getHost()); 87 } 88 89 context.log("Subject: " + message.getSubject()); 90 context.log("MessageID: " + message.getMessageID()); 91 context.log("Received: " + message.getReceivedDate()); 92 context.log("Sent: " + message.getSentDate()); 93 94 Enumeration allHeadersLines = message.getAllHeaderLines(); 95 while(allHeadersLines.hasMoreElements()) { 96 String header = (String ) allHeadersLines.nextElement(); 97 context.log("Header Line:= " + header); 98 } 99 100 101 Enumeration allHeadersEnumeration = message.getAllHeaders(); 102 while(allHeadersEnumeration.hasMoreElements()) { 103 Header header = (Header ) allHeadersEnumeration.nextElement(); 104 context.log("Header: " + header.getName() + "=" + header.getValue()); 105 } 106 107 Address [] to = message.getRecipients(RecipientType.TO); 108 printAddresses(to, "TO: "); 109 Address [] cc = message.getRecipients(RecipientType.CC); 110 printAddresses(cc, "CC: "); 111 Address [] bcc = message.getRecipients(RecipientType.BCC); 112 printAddresses(bcc, "BCC: "); 113 114 115 Flags flags = message.getFlags(); 116 Flag[] systemFlags = flags.getSystemFlags(); 117 for (int i=0;i<systemFlags.length;i++) { 118 context.log("System Flag:" + systemFlags[i]); 119 } 120 String [] userFlags = flags.getUserFlags(); 121 for (int i=0;i<userFlags.length;i++) { 122 context.log("User flag: " + userFlags[i]); 123 } 124 125 String mimeType = message.getContentType(); 126 context.log("Mime type: " + mimeType); 127 if (mimeType == "text/plain") { 128 try { 129 Object content = message.getContent(); 130 context.log("Content: " + content); 131 } catch (IOException e) { 132 e.printStackTrace(); 133 } 134 135 } 136 137 context.log(""); 138 context.log("######## MAIL ENDS"); 139 } 140 141 private final void printAddresses(Address [] addresses, String prefix) { 142 MailetContext context = config.getMailetContext(); 143 for (int i=0;i<addresses.length;i++) { 144 if (addresses[i] instanceof InternetAddress ) { 145 InternetAddress address = (InternetAddress ) addresses[i]; 146 context.log(prefix + address.getPersonal() + "@" + address.getAddress()); 147 } else if (addresses[i] instanceof NewsAddress ) { 148 NewsAddress address = (NewsAddress ) addresses[i]; 149 context.log(prefix + address.getNewsgroup() + "@" + address.getHost()); 150 } 151 } 152 } 153 } 154 | Popular Tags |