Setting reminders
Now that you can create, update, and delete events, you need a way to remind people about them.
Create the reminder.java file, and add the basic class (see Listing 26).
Listing 26. The reminder class
public class Reminder{
public void sendMessage(String reminderTo, String title,
String description, String dateString) {
}
public void sendAllReminders(int eventMonth,
int eventDay, int eventYear) {
}
public static void main (String args[]){
Reminder rem = new Reminder();
}
}
|
Once again, use the main() method to test the code. Before proceeding, make sure that mail.jar and activation.jar are in your class path.
Sending an e-mail using JavaMail is a powerful, yet straightforward process (see Listing 27).
Listing 27. Sending an e-mail
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;
import javax.mail.Message;
public class Reminder{
public void sendMessage(String reminderTo, String title,
String description, String dateString) {
try {
String smtp = "earthlink.net";
String from = "calendar@nicholaschase.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtp);
Session session =
Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom( new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(reminderTo));
message.setSubject(title);
String bodyText = "Just to reminder for you of the "+
"following event to take place on "+
dateString + ":\n\n";
bodyText = bodyText + description;
message.setText(bodyText);
Transport.send(message);
} catch (Exception e){
e.printStackTrace();
}
}
public void sendAllReminders(int eventMonth,
int eventDay, int eventYear) {
}
public static void main (String args[]){
Reminder rem = new Reminder();
rem.sendMessage("questions@nicholaschase.com", "Test event",
"Test description", "8/26/2005");
}
}
|
First, define how and from where you're sending the message. The mail.smtp.host Property defines the Simple Mail Transfer Protocol (SMTP) server that sends the mail. Contact your ISP for information on this value, or simply check the settings on your mail client. Next, create a session and assign the Properties to it.
The next step is to create the actual message. After associating it with the session and, thus, with the Properties, you can set information, such as the from, to, subject, and body. Finally, you can send the message.
Once you can send a single reminder, you can send multiple reminders based on records in the database (see Listing 28).
Listing 28. Setting all reminders
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;
import javax.mail.Message;
public class Reminder{
public static String driver =
"org.apache.derby.jdbc.EmbeddedDriver";
public void sendMessage(String reminderTo, String title,
String description, String dateString) {
...
}
public void sendAllReminders(int eventMonth,
int eventDay, int eventYear) {
try {
Class.forName(driver).newInstance();
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:derby:c:\\derby\\calendar");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM Event "+
"where eventMonth="+eventMonth+
" and eventDay="+eventDay+" and "+
"eventYear="+eventYear);
if (!rs.next()){
System.out.println("There are no events for "+
eventMonth+"/"+eventDay+"/"+eventYear);
} else {
while (rs.next()) {
String title = rs.getString(2);
String description = rs.getString(3);
String remindersTo = rs.getString(4);
String dateString = rs.getString(5)+"/"
+rs.getString(6)+"/"+rs.getString(7);
sendMessage(remindersTo, title, description,
dateString);
}
}
rs.close();
s.close();
conn.close();
try {
DriverManager.getConnection(
"jdbc:derby:;shutdown=true");
} catch (SQLException se) { }
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String args[]){
Reminder rem = new Reminder();
rem.sendMessage("questions@nicholaschase.com", "Test event",
"Test description", "8/26/2005");
rem.sendAllReminders(2,27,2006);
|
There is nothing new here. You're simply creating a connection, creating a query, and looping through the results. In this case, you're sending the data to the sendmessage() method for further processing.



