-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIMailsget.java
executable file
·358 lines (303 loc) · 13.6 KB
/
GUIMailsget.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/*
* Version 3.0
* Matthias Guenther
* mails abrufen - dies war ganz schoen schwierig
*/
public class GUIMailsget extends GUIMain implements ActionListener {
private JList jlist;
private TextArea definitionArea;
private JPanel mailsReceiveComplete, subPanel, fromPanel, toPanel,
messageData, abortPanel;
private JButton abort, rewrite, answer;
private JFrame messageFrame;
private JLabel subLab, subLabOutput, fromLab, fromLabOutput, toLab,
toLabOutput;
// userInfos[2]
private String userId = userInfos[2];
// userInfos[3]
private String userPass = userInfos[3];
// userInfos[4]
//private String connectURL = "pop3.web.de";
private String connectURL = userInfos[4];
private File userName;
// store the messages
private Message[] message;
private boolean checkEqual = false;
private int num;
private Store store;
Folder folder;
public void guiMailsget() {
try {
messageFrame = new JFrame("Mail Informations");
currentUserDir = dir + "Inbox " + userInfos[1];
userName = new File(currentUserDir);
messageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Session session = Session.getInstance(new Properties());
// get a Store object
store = session.getStore("pop3");
store.connect(connectURL, userId, userPass);
// get "INBOX"
folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// this variable counts the message number
int count = folder.getMessageCount();
System.out.println(count + " new messages");
message = folder.getMessages();
// set size of the frame for the messages
messageFrame.setSize(600, 400);
mailsReceiveComplete = new JPanel(new BorderLayout());
mailsReceiveComplete.setSize(300, 100);
// to mark and save new emails
Object elements[][] = new Object[count][4];
for (int j = 0; j < elements.length; j++) {
num = j;
try {
searchLocalDir(userName);
System.out.println("aktuell: " + checkEqual);
} catch (IOException e) {
e.printStackTrace();
}
for (int z = 0; z <= 3; z++) {
if (z == 0 && checkEqual == true) {
elements[j][z] = new Font("TimesRoman", Font.BOLD, 11);
} else if (z == 0) {
elements[j][z] = new Font("TimesRoman", Font.BOLD, 13);
} else if (z == 1 && checkEqual == true) {
elements[j][z] = Color.gray;
} else if (z == 1) {
elements[j][z] = Color.black;
} else if (z == 2 && checkEqual == true) {
elements[j][z] = new DiamondIcon(Color.gray);
} else if (z == 2) {
elements[j][z] = new DiamondIcon(Color.red);
} else {
elements[j][z] = message[j].getSubject() + " " + message[j].getFrom()[0] + " " + message[j].getSentDate();
}
}
checkEqual = false;
}
JFrame frame = new JFrame("Mail List");
Container contentPane = frame.getContentPane();
// create a new listwith the whole emails inside, with the specific
// infos
jlist = new JList(elements);
ListCellRenderer renderer = new ComplexCellRenderer();
jlist.setCellRenderer(renderer);
jlist.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
try {
JList source = (JList) event.getSource();
int number = source.getSelectedIndex();
definitionArea.setText(message[number].getContent().toString());
subLabOutput.setText(message[number].getSubject().toString());
fromLabOutput.setText(message[number].getFrom()[0].toString());
toLabOutput.setText(message[number].getAllRecipients()[0].toString());
// to write that this message was written
FileWriter fw = null;
try {
String tmp = message[number].getSentDate().toString();
String tmpparse = tmp.replaceAll(":", "_");
fw = new FileWriter(currentUserDir + "/" + tmpparse + ".txt");
String tmp1 = "True\n[FROM]: " + (message[number].getFrom()[0].toString() + "\n");
String tmp2 = "[SUBJECT]: " + (message[number].getSubject().toString() + "\n");
String tmp3 = "[CONTENT]: " + (message[number].getContent().toString());
String messageContentTmp = tmp1 + tmp2 + tmp3;
fw.write(messageContentTmp);
tmp = "";
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
});
mailsReceiveComplete.add("North", jlist);
JScrollPane scrollPane = new JScrollPane(jlist);
scrollPane.setSize(100, 300);
contentPane.add(scrollPane, BorderLayout.CENTER);
frame.setSize(450, 400);
frame.setLocation(600, 0);
frame.setVisible(true);
userName.mkdir();
subPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
subLab = new JLabel("[SUBJECT]:");
subLabOutput = new JLabel();
subPanel.add(subLab);
subPanel.add(subLabOutput);
fromPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
fromLab = new JLabel("[FROM]:");
fromLabOutput = new JLabel();
fromPanel.add(fromLab);
fromPanel.add(fromLabOutput);
toPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
toLab = new JLabel("[To]:");
toLabOutput = new JLabel();
toPanel.add(toLab);
toPanel.add(toLabOutput);
abortPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
abort = new JButton("Quit");
abort.addActionListener(this);
rewrite = new JButton("Reanswer");
rewrite.addActionListener(this);
answer = new JButton("Answer");
answer.addActionListener(this);
abortPanel.add(answer);
abortPanel.add(rewrite);
abortPanel.add(abort);
messageData = new JPanel();
messageData.setLayout(new GridLayout(5, 1));
messageData.add(subPanel);
messageData.add(fromPanel);
messageData.add(toPanel);
// mergin all Message Data from abovethe components together
definitionArea = new TextArea();
definitionArea.setEditable(false);
mailsReceiveComplete.add("Center", messageData);
mailsReceiveComplete.add("South", definitionArea);
mailsReceiveComplete.add("East", abortPanel);
messageFrame.add(mailsReceiveComplete);
messageFrame.setVisible(true);
} catch (MessagingException msg) {
System.out.println(msg);
}
}
// search for mails on the account
public void searchLocalDir(File dir) throws IOException {
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
try {
// to parse the Strings
String checktmp = message[num].getSentDate() + ".txt";
String check = checktmp.replaceAll(":", "_");
if (check.equals(files[i].getName())) {
// check if the file starts with True
String strLine = "";
try {
// open file to read it
BufferedReader oRead = new BufferedReader(
new FileReader(currentUserDir + "/" + files[i].getName()));
while (null != (strLine = oRead.readLine())) {
if (strLine.startsWith("True")) {
checkEqual = true;
}
}
oRead.close();
} catch (FileNotFoundException exFileNotFound) {
System.out.println(exFileNotFound);
} catch (IOException exIO) {
System.out.println(exIO);
} catch (Exception eAllg) {
System.out.println(eAllg);
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
// for the buttons to ander
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == abort) {
messageFrame.setVisible(false);
try {
folder.close(false);
store.close();
} catch (MessagingException e1) {
e1.printStackTrace();
}
}
if (obj == rewrite) {
int index = jlist.getSelectedIndex();
try {
// get the contents of the selected message
fromLabOutput.setText(message[index].getFrom()[0].toString());
subLabOutput.setText(message[index].getSubject().toString());
definitionArea.setText(message[index].getContent().toString());
// get the strings of these tmp infos of the messages
String tmp1 = fromLabOutput.getText();
String tmp2 = subLabOutput.getText();
String tmp3 = definitionArea.getText();
GUIMailsend send = new GUIMailsend();
// setting the parameter
send.fromString = tmp1;
send.subjectString = tmp2;
send.contentString = tmp3;
send.toString = "";
send.guiMailsend();
} catch (IOException e1) {
} catch (MessagingException e1) {
}
}
// the same as above
if (obj == answer) {
int index = jlist.getSelectedIndex();
try {
fromLabOutput.setText(message[index].getFrom()[0].toString());
subLabOutput.setText(message[index].getSubject().toString());
definitionArea.setText(message[index].getContent().toString());
// get the strings of these tmp infos of the messages
String tmp1 = fromLabOutput.getText();
String tmp2 = subLabOutput.getText();
String tmp3 = definitionArea.getText();
String tmp4 = message[index].getSentDate().toString();
// create a new object
GUIMailsend send = new GUIMailsend();
String[] ak = tmp3.split("\n");
String content = "";
String tmpp;
for (int i = 0; i < ak.length; i++) {
tmpp = "> " + ak[i];
content = content + tmpp + "\n";
}
String infos = tmp1 + " wrote " + tmp4 + "\n" + content;
send.fromString = tmp1;
send.subjectString = "RE: " + tmp2;
send.contentString = infos;
send.guiMailsend();
} catch (Exception msg) {
System.out.println(msg);
}
}
}
public static void main(String[] args) {
GUIMailsget test = new GUIMailsget();
test.guiMailsget();
}
}