title: How to receive Email in Java EE application
body: <p>Obviously it's not so difficult to send out emails from a Java EE application via JavaMail. What I am interested in is the best pattern to <em>receive</em> emails (notification bounces, mostly)? I am not interested in IMAP/POP3-based approaches (polling the inbox) - my application shall <em>react</em> to inbound emails.</p>
  
  <p>One approach I could think of would be</p>
  
  <ul>
  <li>Keep existing MTA (postfix on linux in my case) -> ops team already knows how to configure / operate it</li>
  <li>For every mail that arrives, spawn a Java app that receives the data and sends it off via JMS. I could do this via an entry in /etc/aliases like <code>myuser: "|/path/to/javahelper"</code> with javahelper calling the Java app, passing STDIN along.</li>
  <li>MDB (part of Java EE application) receives JMS message, parses it, detects bounce message and acts accordingly. </li>
  </ul>
  
  <p>Another approach could be</p>
  
  <ul>
  <li>Open a listening network socket on port 25 on the Java EE application container.</li>
  <li>Associate a SessionBean with the socket. Bean is part of Java EE application and can parse/detect bounces/handle the messages directly.</li>
  <li>Keep existing MTA as inbound relay, do all its security/spam filtering, but forward emails to <code>myuser</code> (that pass the filter) to the Java EE application container, port 25.</li>
  </ul>
  
  <p>The first approach I have done before (albeit in a different language/setup).</p>
  
  <p>From a performance and (perceived) cleanliness point of view, I think the second approach is better, but it would require me to provide a proper SMTP transport implementation. Also, I don't know if it's at all possible to connect a network socket with a bean...</p>
  
  <p>What is your recommendation? Do you have details about the second approach?</p>