aspose file tools*
The moose likes I/O and Streams and the fly likes How to pipe emails to a java file and read? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to pipe emails to a java file and read?" Watch "How to pipe emails to a java file and read?" New topic
Author

How to pipe emails to a java file and read?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hi,

I am not sure if this is in the correct section, so apologies if not.

What I am looking to do it to pipe all emails through a java file and read the data, although I am not sure where to begin with this.

I have had a good look around the internet/forums/tutorials although I haven't been able to find anything specific to this.

So lets say I send an email to "user1@example.com", I then want this email to be forwarded onto my java file "IncomingEmailHandler.class". Now I can set all of this up via my webhosting company (ie piping all emails to a certain script).

Although my issue is how I actually read this incoming data, and how I separate it all out into its relevant parts. I believe this will be something to do with I/O java, but I have no experience of this.

If anyone can point me in the right direction to read some information on this to help me achieve this that would be much appreciated.

On a similar note, if anyone has any ideas how I can actually test this on my local machine without having to keep uploading to my webshost to test that would also be mega useful :-)

Thanks
Michael

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
The IncomingEmailHandler class would use the JavaMail API to fetch incoming mails periodically from the mail account. It has methods that provide access to all parts of an email.

You can use a local mail server (like Apache James) and servlet container (like Tomcat) to test this on your own machine.


Ping & DNS - my free Android networking tools app
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hi Ulf Dittmer, thanks for the quick reply.

I was originally thinking along those same lines, although the method that I require is for handling emails to email addresses that don't actually exist. (at least not in the normal way).

The setup I am looking at is to have no email addresses set up, but actually give several people an email address (a virtual one if you will).
- user1@example.com
- user2@example.com
- user3@example.com

So when someone emails any of the above 'virtual' email addresses, since they don't exist on the mail server they would all be automatically piped to the Java file which would then handle them accordingly, based on who the 'user' was. Basically, forwarding the email on to the users personal email address which is stored in a DB.

So the JavaMail setup would not work in this instance since as there is no actual mailbox where all the emails are stored.

I do have some reasons for doing this in a strange way :-) Instead of setting up actual mailboxes and using JavaMail etc.

Does that make sense what I am trying to do?

Would Apache James allow emails to be piped to a program as in the setup above? If so I will look into getting that set up for the testing environment on my local machine.

Thanks
Michael

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
James is a standard mail server (SMTP, POP, IMAP), so I don't think it would fit here.

The problem is that if you want people to email to these addresses, then you need a mail handling process (SMTP) configured for the example.com domain. If for some reason you don't want that to be an actual mail server (although I don't see why - most mail servers can be configured with a "catch-all" address that receives all mails for which no particular mailbox is configured), then you need to write code that listens on the SMTP port and knows enough about SMTP to be able to accept incoming mails. You should be able to find example code by searching for "java smtp server" or some such phrase. Then you'd have to extend it to do whatever you need done with incoming mails.

I'm curious, though: You mention "webhosting" - does that not come with an SMTP server you could use? It seems that would be a lot easier.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Yes the current web hosting does come with an SMTP server. The issue I would have using this and actually setting up email addresses is that this would require someone to manually create / delete accounts as and when required (which could become quite time consuming longer term). So by having the virtual email addresses, which are acting as a 'catch-all' for all mails where no mailbox is configured, then I can programatically/DB control adding/deleting new virtual email accounts, which all forward the emails on to a users personal email address (which can also be updated via a webapp).

Since JavaMail doesn't have any functions to add/delete email accounts to the email server, or change passwords, then this is not suitable as I want everything to be controlled via a web app.

Although after having a good think about alternative solutions, I believe something like this could work....
1) Forward all 'virtual' email accounts to a single real email address - allemails@example.com
2) Use JavaMail to 'listen' to any new messages that come in
3) Use JavaMail to read the headers of the email, one of which being the original email address the message was supposed to be sent to (ie the virtual email address)
4) Check in DB for this info & forward email on
5) Delete the original email

Looking at the headers that are available in emails I believe this would work.

I will have a play with this, and will also get Apache James set up since this will be querying an email account.

Thanks
Michael

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pipe emails to a java file and read?