HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Fetching emails from various clients to store in a database

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
clientsstoredatabaseemailsfetchingfromvarious

Problem

I have written code which does the following:

-
The main goal is to fetch emails from inbox and spam folders and store them in a database. It fetches emails from Gmail, GMX, web.de, Yahoo and Hotmail.

The following attributes are stored in a MySQL database:

Slno, messagedigest, messageid, foldername, dateandtime, receiver, sender, subject, cc, size and emlfile.

-
For Gmail, GMY and web.de, I have used the JavaMail API because email from it can be fetched with IMAP.

-
For Yahoo and Hotmail, I have used an HTML parser and HTTP client to fetch emails from the spam folder, and for the inbox folder, I have used the POP3 JavaMail API.

I want to have a proper class hierarchy which makes my code efficient and easily reusable. I am sure it can still be improved, so I would like to have different opinions on it.

I have the following classes and methods as of now:

-
MainController - Here I pass emailid, password and foldername from which emails have to be fetched.

-
Abstract class - EmailProtocol

Abstract methods of it (all methods except executeParser contains method definition):

  • connectImap() - used by GMX, Gmail and web.de email IDs



  • connectPop3() - used by Hotmail and Yahoo to fetch emails from the inbox folder



  • createMessageDigest - used by every email provider (GMX, Gmail, web.de, Yahoo, and Hotmail)



  • establishDBConnection - used by every email



  • emailAlreadyExists // used by every email which checks whether email already exists in db or not, if not then store it.



  • storeemailproperties - used by every email to store emails properties to MySQL database



  • executeParser - nothing written in it. Overridden and used by just Hotmail and Yahoo to fetch emails from the spam folder.



-
Imap extends EmailProtocol

There's nothing in it, but I have to have it to access methods of EmailProtocol. This is used to fetch emails from Gmail, GMX and web.de. I know this is really a bad way but don't know how t

Solution

Well, for starters, I would put the mailbox information into a configuration file, properties file, database table, etc. Anywhere besides hard coded.

Second, your main() method is totally out of control and needs to be broken up. I would suggest reading up about refactoring in general, but here is a bit that discusses Extract Method which is one of your more basic and crucially important refactorings.

Context

StackExchange Code Review Q#15480, answer score: 4

Revisions (0)

No revisions yet.