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; 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!"); }
/* Destructor. Closes the connection if something bad happens. */ protected void finalize() throws Throwable { if (isConnected) { close(); } super.finalize(); }