-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTPClient.java
More file actions
423 lines (359 loc) · 14.6 KB
/
SMTPClient.java
File metadata and controls
423 lines (359 loc) · 14.6 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Brandenburg University of Technology Cottbus - Senftenberg (BTU)
* Department of Computer Science, Information and Media Technology
* Computer Networks and Communication Systems Group
*
* Practical course "Introduction to Computer Networks"
* Task 1: Implementation of a Simple SMTP Client
*
* @author: Vitaliy Sobur <soburvit@tu-cottbus.de>
* @author: Michael Vogel <mv@informatik.tu-cottbus.de>
* @version: 2013-10-10
*
* A simple GUI of a primitive SMTP Client.
* Other necessary files: SpringUtilities.java.
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("serial")
public class SMTPClient extends JPanel implements ActionListener {
public final static String subject = "Cicero: \"De finibus bonorum et malorum\", 1.10.32";
public final static String messageString = "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem\n"
+ "accusantium doloremque laudantium, totam rem aperiam eaque ipsa,\n"
+ "quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt,\n"
+ "explicabo. nemo enim ipsam voluptatem, quia voluptas sit, aspernatur\n"
+ "aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione\n"
+ "voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem\n"
+ "ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non\n"
+ "numquam eius modi tempora incidunt, ut labore et dolore magnam\n"
+ "aliquam quaerat voluptatem. ut enim ad minima veniam, quis nostrum\n"
+ "exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea\n"
+ "commodi consequatur? quis autem vel eum iure reprehenderit, qui in ea\n"
+ "voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui\n"
+ "dolorem eum fugiat, quo voluptas nulla pariatur?";
private final String[] labels = { "From: ", "To: ", "Cc: ", "Bcc: ",
"Username: ", "Password: ", "Subject: ", "Message: " };
protected enum InputField {
FROM, TO, CC, BCC, USERNAME, PASSWORD, SUBJECT, MESSAGE
}
protected JComponent[] inputComponents;
protected JCheckBox verboseBox;
/**
* Constructor creates all GUI components and adds them to the content pane.
*/
public SMTPClient() {
super(new SpringLayout()); // Set Layout Manager.
inputComponents = new JComponent[labels.length];
JComponent comp;
// Add input fields to pane.
for (InputField f : InputField.values()) {
JLabel label = new JLabel(labels[f.ordinal()], JLabel.TRAILING);
add(label);
// Create the different input fields.
switch (f) {
case PASSWORD:
comp = new JPasswordField(10);
break;
case MESSAGE:
comp = new JTextArea(messageString, 13, 40);
break;
case SUBJECT:
comp = new JTextField(subject, 10);
break;
default:
comp = new JTextField(10);
}
inputComponents[f.ordinal()] = comp; // Store input field reference.
label.setLabelFor(comp); // Connect "springs" of spring layout.
add(comp);
}
add(new JLabel(" ")); // add some vertical space.
add(new JLabel(" "));
verboseBox = new JCheckBox("verbose");
add(verboseBox);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(this);
sendButton.setActionCommand("send");
sendButton.setToolTipText("Click this button to send a mail.");
add(sendButton);
// Create panel layout using Spring Layout utilities.
SpringUtilities.makeCompactGrid(this, // pane
labels.length + 2, 2, // rows, cols
6, 6, // initX, initY
6, 6); // xPad, yPad
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Praktikum GRN, V1: SMTP-Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
SMTPClient contentPane = new SMTPClient();
contentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(contentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* Action handler is called when the user clicks the button or presses Enter
* in a text field.
*/
public void actionPerformed(ActionEvent e) {
String from, to, cc, bcc, username, password, subject, message;
boolean verbose;
if ("send".equals(e.getActionCommand())) {
from = ((JTextComponent) inputComponents[InputField.FROM.ordinal()])
.getText();
to = ((JTextComponent) inputComponents[InputField.TO.ordinal()])
.getText();
cc = ((JTextComponent) inputComponents[InputField.CC.ordinal()])
.getText();
bcc = ((JTextComponent) inputComponents[InputField.BCC.ordinal()])
.getText();
username = ((JTextComponent) inputComponents[InputField.USERNAME
.ordinal()]).getText();
password = ((JTextComponent) inputComponents[InputField.PASSWORD
.ordinal()]).getText();
subject = ((JTextComponent) inputComponents[InputField.SUBJECT
.ordinal()]).getText();
message = ((JTextComponent) inputComponents[InputField.MESSAGE
.ordinal()]).getText();
verbose = verboseBox.isSelected();
sendMail(from, to, cc, bcc, username, password, subject, message, verbose);
}
}
public void showMessage(String code, String message) {
new ModalDialog(new JFrame(), code, message);
}
public Map<String, String> possibleResponses() {
Map<String, String> responses = new HashMap<>();
responses.put("421", "Service not available. Please try later.");
responses.put("450", "The mailbox of recipient is unavailable. Please check the entered data or try again later.");
responses.put("451", "local error in processing. Please try later.");
responses.put("452", "Requested action not taken: insufficient system storage.");
responses.put("500", "Syntax error, command unrecognised.");
responses.put("501", "Syntax error in the input data. Please check all fields.");
responses.put("502", "Command not implemented.");
responses.put("503", "Bad sequence of commands.");
responses.put("504", "Command parameter not implemented.");
responses.put("521", "The Host does not accept mail. Make sure, that the entered data are correct.");
responses.put("530", "Access denied.");
responses.put("535", "Bad username or password.");
responses.put("550", "The mailbox of recipient is unavailable. Please check the entered data or try again later.");
responses.put("552", "Requested mail action aborted: exceeded storage allocation. Please try again later");
responses.put("553", "Requested action not taken: mailbox name not allowed. Please check the input data.");
responses.put("554", "Transaction failed. Please check the entered data or try again later.");
return responses;
}
public String getResponesCode(String input) {
return input.substring(0, 3);
}
public String getResponesMessage(Map<String, String> responses,
String input) {
String message = possibleResponses().get(getResponesCode(input));
if (message != null) {
return message;
} else {
return "Unknown Error!";
}
}
public void log(String string, boolean verbose, boolean isServer) {
if (verbose) {
if (isServer == true) {
String[] arr;
arr = string.split("\\|");
for (int i = 0; i < arr.length; i ++) {
if (i == 0) {
System.out.println("S: " + arr[i]);
} else {
System.out.println(" " + arr[i]);
}
}
} else {
System.out.println("C: " + string);
}
}
}
public void sendMail(String from, String to, String cc, String bcc,
String username, String password, String subject, String message, Boolean verbose) {
String SMTPServer = "romeo.informatik.tu-cottbus.de";
int SMTPPort = 25;
try {
@SuppressWarnings("resource")
Socket echoSocket = new Socket(SMTPServer, SMTPPort);
// Create a buffered reader for line-oriented reading from the
// socket
BufferedReader in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
// Create a print writer for line-oriented writing to the socket
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
true);
// read first response from the mail server
String reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("220")) {
out.println("EHLO " + SMTPServer);
log("EHLO " + SMTPServer, verbose, false);
// ______________ temporary FIX (code needs to be changed)
reply = "";
for (int i = 0; i < 10; i++) {
reply += "|" + in.readLine();
}
reply = reply.substring(1);
// ______________
log(reply, verbose, true);
if (getResponesCode(reply).equals("250")) {
out.println("AUTH LOGIN");
log("AUTH LOGIN", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if(getResponesCode(reply).equals("334")) {
out.println(Base64Coder.encode(username.getBytes()));
log(Base64Coder.encode(username.getBytes()).toString(), verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("334")) {
out.println(Base64Coder.encode(password.getBytes()));
log(Base64Coder.encode(password.getBytes()).toString(), verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("235")) {
out.println("MAIL FROM: <" + from + ">");
log("MAIL FROM: <" + from + ">", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("250")) {
out.println("RCPT TO: <" + to + ">");
log("RCPT TO: <" + to + ">", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("250")) {
if (cc != null && cc.equals("") == false) {
String[] ccArr = cc.split(",");
for (int i = 0; i < ccArr.length; i ++) {
if (getResponesCode(reply).equals("250")) {
out.println("RCPT TO: <" + ccArr[i].trim() + ">");
log("RCPT TO: <" + ccArr[i].trim() + ">", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
break;
}
}
}
if (bcc != null && bcc.equals("") == false) {
String[] bccArr = bcc.split(",");
for (int i = 0; i < bccArr.length; i ++) {
if (getResponesCode(reply).equals("250")) {
out.println("RCPT TO: <" + bccArr[i].trim() + ">");
log("RCPT TO: <" + bccArr[i].trim() + ">", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
break;
}
}
}
if (getResponesCode(reply).equals("250")) {
out.println("DATA");
log("DATA", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("354")) {
out.println("From: " + from);
out.println("To: " + to);
out.println("CC: " + cc);
out.println("Subject: " + subject);
out.println("Message: " + message);
out.println(".");
String letter = "From: " + from + "\n" +
" To: " + to + "\n";
if (cc != null && cc.equals("") == false) {
letter += " CC: " + cc + "\n";
}
letter += "Subject: "+ subject + "\n" +
"Message: " + message + "\n";
log(letter, verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("250")) {
out.println("QUIT");
log("QUIT", verbose, false);
reply = in.readLine();
log(reply, verbose, true);
if (getResponesCode(reply).equals("221")) {
showMessage(getResponesCode(reply), "Your message was successfully sent!");
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} else {
showMessage(getResponesCode(reply),
getResponesMessage(possibleResponses(), reply));
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
}
}
public static void main(String[] args) throws UnknownHostException,
IOException {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}