We are going to create a custom James matcher. The purpose of the matcher is to lookup the sender of an email in a database containing email addresses and see if they exist in the database, we are going to use this to work out whether the senders of emails are registered or unregistered.
First we need to set up our environment, extract the james.sar file -:
jar -xf james.sar
Copy the appropriate jar files to your classpath -:
cp SAR-INF/lib/james-2.3.1.jar /usr/share/java cp SAR-INF/lib/mail-1.4.jar /usr/share/java cp SAR-INF/lib/mailet-2.3.jar /usr/share/java cp SAR-INF/lib/mailet-api-2.3.jar /usr/share/java
Now create our SenderIsJDBC.java file in ./org/apache/james/transport/matchers/SenderIsJDBC.java
SenderIsJDBC Mailet
package org.apache.james.transport.matchers; import org.apache.mailet.GenericMatcher; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import javax.mail.MessagingException; import java.sql.*; import java.util.Collection; import java.util.StringTokenizer; public class SenderIsJDBC extends GenericMatcher { private Collection senders; private ResultSet rs; public void init() throws javax.mail.MessagingException { } public Collection match(Mail mail) { int count = 0; senders = new java.util.HashSet(); try { Class.forName("com.mysql.jdbc.Driver").newInstance (); String url = "jdbc:mysql://127.0.0.1/james?autoReconnect=true"; Connection conn = DriverManager.getConnection(url, "username", "password"); Statement s = conn.createStatement (); s.executeQuery ("SELECT email FROM regUsers"); ResultSet rs = s.getResultSet (); while (rs.next()) { senders.add(new MailAddress(rs.getString("email"))); } } catch (Exception e) { System.err.println ("Cannot connect to database server"); } if (senders.contains(mail.getSender())) { return mail.getRecipients(); } else { return null; } } }
The above matcher is very simple and was based on the SenderIs matcher included with James. Basically, it connects to the SQL database, does a lookup on the email addresses, and then checks if this list of registered senders contains the sender of the message being processed.
Next we need to check our CLASSPATH is correct -:
export CLASSPATH=$CLASSPATH:/usr/share/java:/usr/share/java/mail.jar:/usr/share/java/ james-2.3.1.jar:/usr/share/java/mailet-2.3.jar:/usr/share/java/mailet-api-2.3.jar
Now we can compile our class and create a jar file ready for use by Apache James -:
javac org/apache/james/transport/matchers/SenderIsJDBC.java jar -cf SenderIsJDBC.jar org/apache/james/transport/matchers/SenderIsJDBC.class
Now copy the finshed JAR file into James’ lib folders -:
cp SenderIsJDBC.jar ../apps/james/SAR-INF/lib cp SenderIsJDBC.jar ../lib
And finally tell James about the new matcher in config.xml -:
<matcherpackages> <matcherpackage>org.apache.james.transport.matchers.SenderIsJDBC</matcherpackage> </matcherpackages>
Now on starting James the spoolmaneger log file should tell you your new matcher has been initialised -:
spoolmanager: Matcher SenderIsJDBC instantiated.
We can now use this matcher using the following in config.xml -:
<mailet match="SenderIsJDBC"> <sender>postmaster@localhost</sender> <reversePath>postmaster@localhost</reversePath> <attachError>false</attachError> <message>User is registered</message> <prefix>[Registered] </prefix> <passThrough>true</passThrough> <fakeDomainCheck>false</fakeDomainCheck> <recipients>sender</recipients> <to>sender</to> <inline>message</inline> <attachment>none</attachment> <isReply>true</isReply> <debug>false</debug> </mailet>
This would result in registered users (emails exists in the database) getting an email letting them know they are registered.
