-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailClient.java
146 lines (131 loc) · 4.77 KB
/
MailClient.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
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MailClient extends Frame {
/* The stuff for the GUI. */
private Button btSend = new Button("Send");
private Button btClear = new Button("Clear");
private Button btQuit = new Button("Quit");
private Label serverLabel = new Label("Local mailserver:");
private TextField serverField = new TextField("", 40);
private Label fromLabel = new Label("From:");
private TextField fromField = new TextField("", 40);
private Label toLabel = new Label("To:");
private TextField toField = new TextField("", 40);
private Label subjectLabel = new Label("Subject:");
private TextField subjectField = new TextField("", 40);
private Label messageLabel = new Label("Message:");
private TextArea messageText = new TextArea(10, 40);
/**
* Create a new MailClient window with fields for entering all
* the relevant information (From, To, Subject, and message).
*/
public MailClient() {
super("Java Mailclient");
/* Create panels for holding the fields. To make it look nice,
create an extra panel for holding all the child panels. */
Panel serverPanel = new Panel(new BorderLayout());
Panel fromPanel = new Panel(new BorderLayout());
Panel toPanel = new Panel(new BorderLayout());
Panel subjectPanel = new Panel(new BorderLayout());
Panel messagePanel = new Panel(new BorderLayout());
serverPanel.add(serverLabel, BorderLayout.WEST);
serverPanel.add(serverField, BorderLayout.CENTER);
fromPanel.add(fromLabel, BorderLayout.WEST);
fromPanel.add(fromField, BorderLayout.CENTER);
toPanel.add(toLabel, BorderLayout.WEST);
toPanel.add(toField, BorderLayout.CENTER);
subjectPanel.add(subjectLabel, BorderLayout.WEST);
subjectPanel.add(subjectField, BorderLayout.CENTER);
messagePanel.add(messageLabel, BorderLayout.NORTH);
messagePanel.add(messageText, BorderLayout.CENTER);
Panel fieldPanel = new Panel(new GridLayout(0, 1));
fieldPanel.add(serverPanel);
fieldPanel.add(fromPanel);
fieldPanel.add(toPanel);
fieldPanel.add(subjectPanel);
/* Create a panel for the buttons and add listeners to the
buttons. */
Panel buttonPanel = new Panel(new GridLayout(1, 0));
btSend.addActionListener(new SendListener());
btClear.addActionListener(new ClearListener());
btQuit.addActionListener(new QuitListener());
buttonPanel.add(btSend);
buttonPanel.add(btClear);
buttonPanel.add(btQuit);
/* Add, pack, and show. */
add(fieldPanel, BorderLayout.NORTH);
add(messagePanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
pack();
show();
}
static public void main(String argv[]) {
new MailClient();
}
/* Handler for the Send-button. */
class SendListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Sending mail");
/* Check that we have the local mailserver */
if ((serverField.getText()).equals("")) {
System.out.println("Need name of local mailserver!");
return;
}
/* Check that we have the sender and recipient. */
if((fromField.getText()).equals("")) {
System.out.println("Need sender!");
return;
}
if((toField.getText()).equals("")) {
System.out.println("Need recipient!");
return;
}
/* Create the message */
Message mailMessage = new Message(fromField.getText(),
toField.getText(),
subjectField.getText(),
messageText.getText());
/* Check that the message is valid, i.e., sender and
* recipient addresses look ok. */
if(!mailMessage.isValid()) {
return;
}
/* Create the envelope, open the connection and try to send
* the message. */
Envelope envelope = null;
try {
envelope = new Envelope(mailMessage, serverField.getText());
} catch (UnknownHostException e) {
/* If there is an error, do not go further */
return;
}
try {
SMTPConnection connection = new SMTPConnection(envelope);
connection.send(envelope);
connection.close();
} catch (IOException error) {
System.out.println("Sending failed: " + error);
return;
}
System.out.println("Mail sent succesfully!");
}
}
/* Clear the fields on the GUI. */
class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Clearing fields");
fromField.setText("");
toField.setText("");
subjectField.setText("");
messageText.setText("");
}
}
/* Quit. */
class QuitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}