Although you may simply be interested in knowing who reads your home page, the main reason for users to register themselves may be that they can get a message whenever you update your page. This will be implemented in this exercise.
The server will not send mail automatically when your web page changes. It could do this, by creating a new background thread that continuously checks your home page for changes, but this would mean that mails would be sent every time you change your home page, even if you only made a minor cosmetic change. Instead, you will add a new command to your server for sending mail to all registered users that indicated that they want to be notified when your home page changes; this command must take the actual message to be sent as an argument. (Previously, you had implemented the ``submit'' command, which had a UserInfo as an ``argument''.)
Since you don't want ordinary users to access this command, you will also need to create a new applet that can send the ``send mail'' command to the server. This applet should have at least a text field for the subject, a text area for the actual message, and a button that sends the information to the server, telling it to send the message to all users.
Of course, you will also need to create a new HTML page containing your applet. As in the previous lab, you can put your code in a JAR archive.
Unfortunately, Java currently does not have any built-in methods for sending e-mail. This may change in the future with the JavaMail API, but currently, we must connect to an SMTP server ``manually''. You can do this with the class defined below. Note that this implementation is very ugly: It does not check any error messages sent by the SMTP server, and therefore we don't know whether our mails were accepted or not.
In this case, you can send mail messages to the mail.student.liu.se SMTP server.
package common;
import java.lang.*;
import java.io.*;
import java.net.*;
public class MailSender
{
private String serverAddress;
/**
* Create a new mail sender for the given server address; you can create
* this object once in your server and re-use it in all threads.
*/
public MailSender(String serverAddress) {
this.serverAddress = serverAddress;
}
/**
* @param from The sender's mail address
* @param to The recipient's mail address
* @param subject The subject of the message
* @param message The actual message
*
* @return null if OK, otherwise a String (an error message).
*/
public String send(String from, String to, String subject, String message) {
Socket socket;
// Port 25 is the SMTP port.
try {
socket = new Socket(serverAddress, 25);
} catch (IOException e) {
return "Cannot connect to mail host '" + serverAddress + "': " + e;
}
PrintStream out;
try {
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
return "Cannot get output stream to mail host '" + serverAddress + "': " + e;
}
try {
out.println("HELO " + InetAddress.getLocalHost().getHostName().toString());
out.println("MAIL FROM: " + from);
out.println("RCPT TO: " + to);
out.println("DATA");
out.println("From: " + from);
out.println("To: " + to);
out.println("Subject: " + subject);
out.println("");
out.println(message);
out.println(".");
out.println("QUIT");
} catch (IOException e) {
return "Error sending mail: " + e;
}
return null;
}
}