- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmail.java
More file actions
Latest commit
88 lines (70 loc) · 1.87 KB
/
Copy pathEmail.java
File metadata and controls
88 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
importjava.util.Properties;
importjavax.mail.Folder;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.Session;
importjavax.mail.Store;
importjavax.mail.URLName;
publicclassEmail {
privatestaticSessionsession;
privatestaticStorestore;
privatestaticFolderfolder;
// hardcoding protocol and the folder
// it can be parameterized and enhanced as required
privatestaticStringprotocol = "imaps";
privatestaticStringfile = "INBOX";
publicEmail() {
}
publicbooleanisLoggedIn() {
returnstore.isConnected();
}
/**
* to login to the mail host server
*/
publicstaticvoidlogin(Stringhost, Stringusername, Stringpassword) throwsException {
URLNameurl = newURLName(protocol, host, 993, file, username, password);
if (session == null) {
Propertiesprops = null;
try {
props = System.getProperties();
} catch (SecurityExceptionsex) {
props = newProperties();
}
session = Session.getInstance(props, null);
}
store = session.getStore(url);
store.connect();
folder = store.getFolder(url);
folder.open(Folder.READ_WRITE);
}
/**
* to logout from the mail host server
*/
publicstaticvoidlogout() throwsMessagingException {
folder.close(false);
store.close();
store = null;
session = null;
}
publicstaticintgetMessageCount() {
intmessageCount = 0;
try {
messageCount = folder.getMessageCount();
} catch (MessagingExceptionme) {
me.printStackTrace();
}
returnmessageCount;
}
publicstaticMessage[] getMessages() throwsMessagingException {
returnfolder.getMessages();
}
publicstaticvoidmain(String[] args) throwsException {
login("imap.gmail.com", "email", "password");
System.out.println(getMessageCount());
Message[] message = getMessages();
for(Messagem: message) {
System.out.println(m.getSubject());
}
logout();
}
}