Thursday, May 22, 2014

Process bounced messages through BoogieTools in JAVA

Here is a sample JAVA code to process bounced messages in a mail server through BoogieTools API.
You need to have the BounceStudio so files in your class path while executing the code.

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;

import org.apache.commons.io.IOUtils;

import com.boogietools.bouncestudio.BounceStudio;
import com.boogietools.bouncestudio.BounceStudio.BounceResult;
import com.sun.mail.imap.IMAPMessage;


public class BoogieToolsBounceProcessor
{
    private static final Map<Integer, String> BOUNCE_CODES_MAP;

    static
    {
        Map<Integer, String> aMap = new HashMap<Integer, String>();
        aMap.put(10, "HARD_BOUNCE_USER_UNKNOWN");// "HARD BOUNCE - (ie. User Unknown)");
        aMap.put(20, "SOFT_BOUNCE_OTHER");// "SOFT BOUNCE - General");
        aMap.put(21, "SOFT_BOUNCE_DOMAIN_FAILURE");// "SOFT BOUNCE - Dns Failure");
        aMap.put(22, "SOFT_BOUNCE_MAILBOX_FULL");// "SOFT BOUNCE - Mailbox Full");
        aMap.put(23, "SOFT_BOUNCE_MESSAGE_TOO_LARGE");// "SOFT BOUNCE - Message Too Large");
        aMap.put(30, "HARD_BOUNCE_NO_EMAIL_ADDRESS");// "BOUNCE - NO EMAIL ADDRESS. VERY RARE!");
        aMap.put(40, "UNKNOWN_BOUNCE_UNKNOWN");// "GENERAL BOUNCE");
        aMap.put(50, "BLOCK BOUNCE - BLOCKED");// "MAIL BLOCK - General");
        aMap.put(51, "BLOCK_BOUNCE_KNOWN_SPAMMER");// "MAIL BLOCK - Known Spammer");
        aMap.put(52, "BLOCK BOUNCE - SPAMBLOCKED");// "MAIL BLOCK - Spam Detected");
        aMap.put(53, "BLOCK_BOUNCE_CONTENT");// "MAIL BLOCK - Attachment Detected");
        aMap.put(54, "BLOCK_BOUNCE_RELAY_DENIED");// "MAIL BLOCK - Relay Denied");
        BOUNCE_CODES_MAP = Collections.unmodifiableMap(aMap);
    }


    public static void main(String[] args)
    {
        boolean isDebug = false;
        if (args.length < 2) {
            System.out.println("Usage: BoogieToolsTest <mail_host> <mail_user> <mail_password>");
            System.exit(1);
        }

        try
        {
            String host = args[0];
            String user = args[1];
            String password = args[2];
            System.out.println("Processing bounced messages in "+host);
   
            Properties props = System.getProperties();
            props.setProperty("mail.store.protocol", "imap");
   
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imap");
            store.connect(host, user, password);
   
            Folder inbox = store.getFolder("Inbox");
            int unreadCount = inbox.getUnreadMessageCount();
            System.out.println("There are "+unreadCount+" to be processed");
            inbox.open(Folder.READ_WRITE);
            Message[] messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
            for (Message message : messages) {
                ((IMAPMessage)message).setPeek(isDebug);
                String bounceReasonCode = getBounceReason(message);
               
                if (bounceReasonCode != null)
                {
                    System.out.println("Bounce Code: "+ bounceReasonCode);
                    //Do something
                }
            }
        }catch(Exception e){}
        System.out.println("Finished processing bounced messages");
    }
    private static String getBounceReason(Message message)
    {
        try
        {
            String messageText = IOUtils.toString(message.getInputStream());
            System.out.println("Going to make Boogietools call");
            BounceResult bresult = BounceStudio
                        .bounceCheck(messageText, "",
                                "your_original_or_trial_license_key_goes_here");
            System.out.println("Return from Boogietools: "
                      + bresult.getResult());
            return BOUNCE_CODES_MAP.get(bresult.getResult());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (MessagingException e)
        {
            e.printStackTrace();
        }

        return null;
    }
}