/Users/lyon/j4p/src/net/mail/swing/SendMailApplet.java
|
1 package net.mail.swing;
2
3 import ip.gui.frames.ClosableFrame;
4
5 import java.applet.Applet;
6 import java.awt.*;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import java.io.PrintWriter;
13 import java.net.InetAddress;
14 import java.net.Socket;
15 import java.util.Date;
16
17
18 public class SendMailApplet extends Applet {
19
20 //
21 // The e-mail addBk.address all messages are to be sent to; specified in HTML
22 //
23 private String recipientEmailAddress = null;
24
25 private String emailServerHostName = null;
26 private boolean standalone = false;
27 private int smtpPort = 25;
28 private Socket socket;
29 private PrintWriter pw;
30 private InetAddress remoteInternetAddress;
31 private InetAddress localInternetAddress;
32
33 private EmailClientGui form;
34
35
36 /**
37 * Initialize the applet.
38 */
39 public void init() {
40
41 setBackground(Color.white);
42 form = new net.mail.swing.EmailClientGui(this);
43 add(form);
44 setSize(600, 450);
45 if (emailServerHostName == null)
46 emailServerHostName = getCodeBase().getHost();
47 if (recipientEmailAddress == null)
48 recipientEmailAddress = getParameter("RECIPIENT");
49 }
50
51
52 /**
53 * Show status to the user.
54 */
55 public void showStatus(String s) {
56
57 System.out.println(s);
58 if (standalone) return;
59 super.showStatus(s);
60 }
61
62
63 private BufferedReader br;
64
65 /**
66 * Send an e-mail message.
67 */
68 public void send() throws IOException, Exception {
69
70 // Open connection to SMTP server
71 socket = new Socket(emailServerHostName, smtpPort);
72
73 // Send the form contents as a message
74 try {
75 remoteInternetAddress =
76 socket.getInetAddress();
77 localInternetAddress =
78 remoteInternetAddress.getLocalHost();
79
80 //
81 // Conversion of the PrintStream due to deprication.
82 // ps = new PrintStream(socket.getOutputStream());
83 //
84 // Print values and objects to a Writer.
85 //
86 pw = new PrintWriter(socket.getOutputStream());
87
88 //
89 // Conversion of the DataInputStream due to deprication.
90 // dis = new DataInputStream(socket.getInputStream());
91 //
92 // Use BufferedReader instead of DataInputStream.
93 //
94 br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
95
96 // Send message
97 sendline("HELO " + localInternetAddress.toString());
98 sendline("MAIL FROM:" + form.email());
99 sendline("RCPT TO:" + recipientEmailAddress);
100 sendline("DATA");
101 sendline(form.message());
102 sendline(".");
103 } catch (Exception ex) {
104 socket.close();
105 throw ex;
106 }
107
108 // Close connection
109 socket.close();
110 }
111
112
113 /**
114 * Send a line of data to the server, and retrieve the handshake
115 */
116 void sendline(String data) throws IOException {
117
118 System.out.println("sendline out:" + data);
119 pw.println(data);
120 pw.flush();
121 //
122 // Conversion of the DataInputStream due to deprication.
123 //
124 // String s = dis.readLine();
125 //
126 String s = br.readLine();
127 System.out.println("sendline in:" + s);
128 }
129
130
131 /**
132 * Main routine, for standalone program execution
133 */
134 public static void main(String args[]) {
135
136 SendMailApplet ap = new SendMailApplet();
137 // The server host will be the place running POP
138 // webmaster e-mail will be recipient
139 ap.emailServerHostName = "192.168.1.96";
140 ap.recipientEmailAddress = "lyon@docjava.com";
141 ap.standalone = true;
142
143 ClosableFrame fr = new ClosableFrame("SendMail");
144 ap.init();
145 fr.add("Center", ap);
146 fr.setSize(600, 450);
147
148 fr.show();
149 ap.start();
150 }
151
152 public String getWebmasterEmail() {
153 return recipientEmailAddress;
154 }
155
156 public void setWebmasterEmail(String webmasterEmail) {
157 this.recipientEmailAddress = webmasterEmail;
158 }
159
160 public String getServerHostName() {
161 return emailServerHostName;
162 }
163
164 public void setServerHostName(String serverHostName) {
165 this.emailServerHostName = serverHostName;
166 }
167
168 public boolean isStandalone() {
169 return standalone;
170 }
171
172 public void setStandalone(boolean standalone) {
173 this.standalone = standalone;
174 }
175
176 public int getSmtpPort() {
177 return smtpPort;
178 }
179
180 public void setSmtpPort(int smtpPort) {
181 this.smtpPort = smtpPort;
182 }
183
184 public Socket getSocket() {
185 return socket;
186 }
187
188 public void setSocket(Socket socket) {
189 this.socket = socket;
190 }
191
192 public PrintWriter getPs() {
193 return pw;
194 }
195
196 public void setPs(PrintWriter ps) {
197 this.pw = ps;
198 }
199
200 public InetAddress getRina() {
201 return remoteInternetAddress;
202 }
203
204 public void setRina(InetAddress rina) {
205 this.remoteInternetAddress = rina;
206 }
207
208 public InetAddress getLina() {
209 return localInternetAddress;
210 }
211
212 public void setLina(InetAddress lina) {
213 this.localInternetAddress = lina;
214 }
215
216 public net.mail.swing.EmailClientGui getForm() {
217 return form;
218 }
219
220 public void setForm(net.mail.swing.EmailClientGui form) {
221 this.form = form;
222 }
223
224 public BufferedReader getBuf_reader() {
225 return br;
226 }
227
228 public void setBuf_reader(BufferedReader buf_reader) {
229 this.br = buf_reader;
230 }
231 }
232
233
234 /**
235 * A form for obtaining user input. Customize this for your application, just
236 * as you would customize an HTML form for a Web-based e-mail application.
237 */
238 class EmailClientGui extends Panel implements ActionListener {
239
240 SendMailApplet applet;
241
242 // The form's elements...
243 Label nameLabel;
244 TextField nameField;
245 Label emailLabel;
246 TextField emailField;
247 Label orgLabel;
248 TextField orgField;
249 Label msgBodyLabel;
250 TextArea msgBodyArea;
251 Button sendButton;
252
253
254 /**
255 * The constructor
256 */
257 public EmailClientGui(SendMailApplet ap) {
258
259 applet = ap;
260 setBackground(Color.white);
261 setLayout(new GridLayout(2, 1));
262
263 // Create a panel to put the text fields and button on
264 Panel p = new Panel();
265 p.setLayout(new GridLayout(8, 1));
266
267 // Instantiate all the elements, and add them to their containers...
268 p.add(sendButton = new Button("Send"));
269 sendButton.addActionListener(this);
270 p.add(nameLabel = new Label("Your Name:"));
271 p.add(nameField = new TextField(60));
272 p.add(emailLabel = new Label("Your e-mail addBk.address:"));
273 p.add(emailField = new TextField(60));
274 p.add(orgLabel = new Label("Your orgainization:"));
275 p.add(orgField = new TextField(60));
276 p.add(msgBodyLabel = new Label("Your Message:"));
277 add(p);
278 add(msgBodyArea = new TextArea(3, 60));
279
280 // Set the size of the form
281 setSize(550, 500);
282 }
283
284
285 /**
286 * Return the value in the e-mail addBk.address field in the form
287 */
288 public String email() {
289 return emailField.getText();
290 }
291
292
293 //
294 // Updated to the new Event model.
295 //
296 public void actionPerformed(ActionEvent e) {
297
298 if (e.getSource() == sendButton) {
299
300 //
301 // User clicked the Send button; send the message
302 //
303 try {
304 applet.send();
305 } catch (Exception ex) {
306 applet.showStatus("Error; message send failed:\n " + ex.toString());
307
308 return;
309 }
310 applet.showStatus("Message sent");
311
312 return;
313 }
314 }
315
316
317 /**
318 * Return the contents of the body of the form, including any "hidden" fields.
319 */
320 public String message() {
321
322 String m = "";
323
324 m += nameLabel.getText();
325 m += nameField.getText();
326 m += "\n";
327
328 m += orgLabel.getText();
329 m += orgField.getText();
330 m += "\n";
331
332 m += "Web Origin:";
333 if (!applet.isStandalone()) m += applet.getDocumentBase();
334 m += "\n";
335
336 m += "Date Sent:";
337 m += (new Date()).toString();
338 m += "\n";
339
340 m += msgBodyLabel.getText();
341 m += msgBodyArea.getText();
342 m += "\n";
343
344 return m;
345 }
346 } // end Form class
347