package notification;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class Notification{
/** Creates a new instance of Notification*/
public Notification() {
}
public static void main(String[] args) {
try {
String sql = "Select name, email from users where DATEDIFF(NOW(),birthday)=0)";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/birthday?user=java&password=javajava");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String theText = "Congratulations "+rs.getString("name");
new BirthdayMail("smtprelay.myserver.net", rs.getString("email"), "no-reply@myserver.net", "Automatic notification", theText, "Birthday");
}
} catch (SQLException ex) { // handle the error
System.out.println("SQLState: " + ex.getSQLState());
System.out.println(
"VendorError: " + ex.getErrorCode() + " " + ex.getMessage());
} catch (Exception ex) { // handle the error
System.out.println("Non-sql exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
/*
* BirthdayMail.java
*
*
*
*/
package notification;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class BirthdayMail {
/**
* Main method to send a message given on the command line.
*/
public static void main(String args[]) {
try {
String smtpServer=args[0];
String to=args[1];
String from=args[2];
String subject=args[3];
String body=args[4];
send(smtpServer, to, from, subject, body, "");
} catch (Exception ex) {
ex.printStackTrace();
}
System.exit(0);
}
/**
* Constructor.
*/
public BirthdayMail(String s1, String s2, String s3, String s4, String s5, String s6) {
try {
String smtpServer=s1;
String to=s2;
String from=s3;
String subject=s4;
String body=s5;
String personal = s6;
send(smtpServer, to, from, subject, body, personal);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* "send" method to send the message.
*/
public static void send(String smtpServer, String to, String from
, String subject, String body, String personal) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from, personal));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- Set the subject and body text --
msg.setSubject(subject);
//msg.setText(body);
msg.setContent(body, "text/html");
// -- Set some other header information --
msg.setHeader("X-Mailer", "NotificationMail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
//log.info("Message sent OK.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static boolean statusSend(String smtpServer, String to, String from
, String subject, String body, String personal) {
try {
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from, personal));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// -- Set the subject and body text --
msg.setSubject(subject);
//msg.setText(body);
msg.setContent(body, "text/html");
// -- Set some other header information --
msg.setHeader("X-Mailer", "NotificationMail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
return true;
} catch (MessagingException ex){
log.error(ex.toString());
return false;
} catch (Exception ex){
ex.printStackTrace();
return false;
}
}
}
and in cron.daily a script to daily execute the project:
#!/bin/sh
/usr/local/jdk1.5.0_06/bin/java -jar /home/admin/proj/notification/Notification.jar
exit 0