I have written a java code which does following:-
Main goal is to fetch emails from (inbox, spam) folders and store them in database. It fetches emails from gmail,gmx,web.de,yahoo and Hotmail. Following attributes are stored in mysql database.
Slno, messagedigest, messageid, foldername, dateandtime, receiver, sender, subject, cc, size and emlfile.
For gmail,gmy and web.de, I have used javamail API, because email form it can be fetched with IMAP.
For yahoo and hotmail, I have used html parser and httpclient to fetch emails form spam folder and for inbox folder, I have used pop3 javamail API.
I want to have proper class hierarchy which makes my code efficient and easily reusable. As of now I have designed class hierarchy as below: I am sure it can still be improved. So I would like to have different opinions on it.
I have 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 of inbox folder
createMessageDigest // used by every email provider(gmx, gmail,web.de,yahoo,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. Overwridden and used by just hotmail and yahoo to fetch emails form spam folder.
Imap extends EmailProtocol
(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 to do it other way.
Hotmsil extends EmailProtocol
Methods:-
executeParser() :- This is used by just hotmail email id.
fetchjunkemails() :- This is also very specific for only hotmail email id.
Yahoo extends EmailProtocol
Methods:-
executeParser()
storeEmailtotemptable()
MoveEmailtoInbox()
getFoldername()
nullorEquals()
All above methods are specific for yahoo email id.
public DateTimeFormat(class)
format() //this formats datetime of gmax,gmail and web.de emails.
formatYahoodate //this formats datetime of yahoo email.
formatHotmaildate // this formats datetime of hotmail email.
public StringFormat
ConvertStreamToString() // Accessed by every class
except DateTimeFormat class.
formatFromTo() // Accessed by every class
except DateTimeFormat class.
public Class CheckDatabaseExistance
public static void checkForDatabaseTablesAvailability()
(This method checks at the beginnning whether database and required tables exist in mysql or not. if not it creates them)
Please see code of my MainController class so that You can have an idea about how I use different classes.
public class MainController {
public static void main(String[] args) throws Exception {
ArrayList<String> web_de_folders = new ArrayList<String>();
web_de_folders.add("INBOX");
web_de_folders.add("Unbekannt");
web_de_folders.add("Spam");
web_de_folders.add("OUTBOX");
web_de_folders.add("SENT");
web_de_folders.add("DRAFTS");
web_de_folders.add("TRASH");
web_de_folders.add("Trash");
ArrayList<String> gmx_folders = new ArrayList<String>();
gmx_folders.add("INBOX");
gmx_folders.add("Archiv");
gmx_folders.add("Entwürfe");
gmx_folders.add("Gelöscht");
gmx_folders.add("Gesendet");
gmx_folders.add("Spamverdacht");
gmx_folders.add("Trash");
ArrayList<String> gmail_folders = new ArrayList<String>();
gmail_folders.add("Inbox");
gmail_folders.add("[Google Mail]/Spam");
gmail_folders.add("[Google Mail]/Trash");
gmail_folders.add("[Google Mail]/Sent Mail");
ArrayList<String> pop3_folders = new ArrayList<String>();
pop3_folders.add("INBOX");
CheckDatabaseExistance.checkForDatabaseTablesAvailability();
EmailProtocol imap = new Imap();
System.out.println("CHECKING FOR NEW EMAILS IN WEB.DE...(IMAP)");
System.out.println("*********************************************************************************");
imap.connectImap("
[email protected]", "pwd", web_de_folders);
System.out.println("\nCHECKING FOR NEW EMAILS IN GMX.DE...(IMAP)");
System.out.println("*********************************************************************************");
imap.connectImap("
[email protected]", "pwd", gmx_folders);
System.out.println("\nCHECKING FOR NEW EMAILS IN GMAIL...(IMAP)");
System.out.println("*********************************************************************************");
imap.connectImap("
[email protected]", "pwd", gmail_folders);
EmailProtocol yahoo = new Yahoo();
Yahoo y=new Yahoo();
System.out.println("\nEXECUTING YAHOO PARSER");
System.out.println("*********************************************************************************");
y.executeParser("http://de.mc1321.mail.yahoo.com/mc/welcome?ymv=0","
[email protected]","pwd");
System.out.println("\nCHECKING FOR NEW EMAILS IN INBOX OF YAHOO (POP3)");
System.out.println("*********************************************************************************");
yahoo.connectPop3("
[email protected]","pwd",pop3_folders);
System.out.println("\nCHECKING FOR NEW EMAILS IN INBOX OF HOTMAIL (POP3)");
System.out.println("*********************************************************************************");
yahoo.connectPop3("
[email protected]","pwd",pop3_folders);
EmailProtocol hotmail = new Hotmail();
Hotmail h=new Hotmail();
System.out.println("\nEXECUTING HOTMAIL PARSER");
System.out.println("*********************************************************************************");
h.executeParser("https://login.live.com/ppsecure/post.srf","
[email protected]","pwd");
}
}
I have kept DatetimeFormat and StringFormat class public so that I can access its public methods by just (DatetimeFormat.formatYahoodate for e.g. from different methods).
This is the first time I have developed something in java. It serves its purpose but of course code is still not so efficient I think.
I need your suggestions on this project.