-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailApi.java
executable file
·120 lines (97 loc) · 3.62 KB
/
MailApi.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.*;
import java.io.*;
import javax.mail.*;
/**
* @since 04.05.2007
* @author Matthias Guenther
* @version 1.0
* get emails with Mailapi
*/
public class MailApi {
// user informations
private String userId;
private String userPass;
private String connectURL;
private boolean checkOn = true;
// Readers are visible for all methods
private BufferedReader user;
public void getConnection() {
try {
System.out.println("Insert the adress of the POP3-Server: ");
user = new BufferedReader(new InputStreamReader(System.in));
// Beispiel: pop3.web.de
connectURL = user.readLine();
getUser();
} catch (Exception msg) {
System.out.println("\nHost is not available - try again later");
}
}
public void getUser() {
try {
System.out.println("\nInput your username");
System.out.println("---------------------");
userId = user.readLine();
getUserPass();
} catch (Exception msg) {
}
}
public void getUserPass() {
try {
System.out.println("\nInput your password");
System.out.println("---------------------");
userPass = user.readLine();
terminalApi();
} catch (Exception msg) {
}
}
public void terminalApi() {
try {
Session session = Session.getInstance(new Properties());
// get a Store object
Store store = session.getStore("pop3");
store.connect(connectURL, userId, userPass);
// get "INBOX"
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
int count = folder.getMessageCount();
System.out.println(count + " new messages");
Message[] message = folder.getMessages();
Message msg;
for (int i = 1; i <= message.length; i++) {
msg = message[i - 1];
System.out.println("Message number: " + i);
System.out.println("Date: " + msg.getSentDate());
}
do {
System.out.println("Which Message do you want to read? (Quit with Q)");
String terminalInput = user.readLine();
if (terminalInput.startsWith("Q")) {
System.out.println("Sayonara");
checkOn = false;
}
int number = Integer.parseInt(terminalInput);
if (number <= count && number > 0) {
Message m = folder.getMessage(number);
System.out.println("\nMessage number : " + m.getMessageNumber());
System.out.println("Subject : " + m.getSubject());
System.out.println("From : " + m.getFrom()[0]);
System.out.println("Content: " + m.getContent());
System.out.println(m.getSize());
} else {
System.out.println("Your Input is invalid");
}
} while (checkOn == true);
// keep messages on server
folder.close(false);
store.close();
} catch (IOException msg) {
System.out.println(msg);
} catch (MessagingException msg) {
System.out.println(msg);
}
}
public static void main(String[] args) {
MailApi userOne = new MailApi();
userOne.getConnection();
}
}