I didn't understand your full requirements. But let me make the following comment from what all I understood.
If the mail protocol is POP(PostOffice Protocol), then there are components which can retrieve them.
JMail enterprise edition offers one such feature. You can connect to the mail server, download the mail, download attachements and store on your hosting server. The content of the mail you can get as a property of this component. Then you can write them to text file or store in a database (better). The attachments can also be downloaded and renamed.
Visit
http://www.dimac.net to know more about JMail.
ps. The free JMail personal edition does not offer this feature.