Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated processing of an Email in Java

Just got a request from my boss for an application I'm working on. Basically we're getting an email address setup for an external client to submit excel files to.

What I need is a way to automatically pick up any email sent to this address, so I can take the attachment, process it and save it to a folder.

Any information of even where to start would be helpful.\

Note: We're using a lotus notes server to do this, but a generic way would be more helpful (If possible).

like image 732
Galbrezu Avatar asked Dec 10 '22 23:12

Galbrezu


2 Answers

Email -> mailserver ->[something] -> file-on-disk.

File on disk is pretty easy to parse, use JavaMail.

The [something] could be:

  • listener for smtp connections (overkill)!
  • Pop3/imap client
  • Maildir/Mailbox
like image 152
svrist Avatar answered Feb 05 '23 10:02

svrist


Edit: since I first wrote this answer, Wiser has moved and now claims to only be a unit testing tool, so take the answer below with a pinch of salt...


Svrist's answer is good, but if you want to avoid his middle step (the mailserver that writes the mail to disk for later pickup by the Java system) you can use Wiser.

Wiser lets you start an in-Java mailserver:

Wiser wiser = new Wiser();
wiser.setPort(2500);
wiser.start();

Then you can just poll it periodically for mail:

for (WiserMessage message : wiser.getMessages())
{
    String envelopeSender = message.getEnvelopeSender();
    String envelopeReceiver = message.getEnvelopeReceiver();

    MimeMessage mess = message.getMimeMessage();

    // mail processing goes here
}
like image 32
Dan Vinton Avatar answered Feb 05 '23 11:02

Dan Vinton