OOP-ResearchMake It Simpler by Object Oriented Programming

OOP MailScheduler API: How to send e-mails in a Thread Safe way by JavaMail API / Send e-mails from Servlet/JSP at the scheduled time with the given Time Zone (TimeZone) / Object Serialization

How to send e-mails by JavaMail API from Servlets/JSP. How to send e-mails at the scheduled time with the given Time Zone (Java TimeZone). How to send e-mails from Servlet/JSP in a Thread Safe way.
As you know, JavaMail API makes your JSP/Servlets mail ready. But, you may want to send the e-mails at the scheduled time in the future, rather than send the e-mails immediately. By OOP MailScheduler, a few lines of code are enough for your JSP/Servlets on Apache Tomcat to send the e-mails at the scheduled time using JavaMail API. The scheduled time can be specified by any Time Zone (Java TimeZone).
The e-mail will be stored into the SQL table by Object Serialization. Then, at the scheduled time, the back-ground Thread will select it and send it to the SMTP server through JavaMail API. If the communication with the SMTP server fails, the back-ground Thread will retry to send the e-mail upto the specified count. In addition, you can cancel to send the queued e-mail.

Even if you'd like to send the e-mails immediately, it is desirable to avoid the over-head between JavaMail API and the SMTP server. Before your JSP/Servlets sends its response back to the web browser, the JSP/Servlets needs to wait the response from the SMTP server. This over-head will be apparent when the multiple requests arrive in your JSP/Servlets concurrently.
Moreover, the number of connections to the SMTP server may exceeds the maximum number to be allowed. In this situation, some request to your JSP/Servlets on Apache Tomcat may fail to send the e-mails.

OOP MailScheduler ensures the sequential access to the SMTP server, which avoids the over-head and makes your JSP/Servlets on Apache Tomcat truly Thread Safe. Take the full advantage of JavaMail API by the help of OOP MailScheduler.

Putting it all togather : OOP Bento framework

This Java API is now a part of OOP Bento framework. In this MVC framework by XML, you can write all the GUIs in the web application in the normal HTML, XHTML, WML, HDML or any other mark-up languages. And you no longer need to bother about the user authentication and the session tracking. Because its full working source code example will be the good starting point of this Java API, please also visit the web site of Bento framework.

If you'd like to send e-mails immediately... :

If you'd like to send e-mails immediately, there is another Java API for you. It is much simpler API, and a few lines of code will be enough for your JSP/Servlet to send e-mails with the file attachment.

Related Pages:


What is it?

OOP MailScheduler is the Java API for your JSP/Servlets on Apache Tomcat to send the e-mails by JavaMail API.
By its easy-programming interface, a few lines of code are enough for your JSP/Servlets on Apache Tomcat to send the e-mail at the scheduled time in the future.

And even if you'd like to send the e-mails immediately from your JSP/Servlets on Apache Tomcat, this Java API makes your JSP/Servlets more sophisticated ones, i.e. You can make your JSP/Servlets truly Thread Safe. For example, when your JSP/Servlets send the e-mails, you might experience the over-head due to the slow response from SMTP server. Yes, your JSP/Servlets on Apache Tomcat cannot send their response to the browser until the SMTP server sends the response. This over-head may be apparent when the multiple requests arrive in your JSP/Servlets concurrently.

Over head on SMTP
Moreover, the number of connections to the SMTP server may exceeds the maximum number to be allowed. In this situation, some request to your JSP/Servlets may fail to send the e-mails:
But, don't worry. Voilà, OOP MailScheduler! By the help of OOP MailScheduler, all you have to do are:
  1. Set the properties of the e-mail, such as TO address, subject, file attachment and so on.
  2. Specify the scheduled time by the required fields, such as year, hour, Time Zone (TimeZone) and so on
  3. Queue the e-mail
Then, the e-mail will be stored into the SQL table by the Object Serialization. At the scheduled time, the back-ground Thread will select the e-mail from the SQL table and send it to the SMTP server. If the communications with the SMTP server fails, the back-ground Thread will continue to send the e-mail upto the specified count.

You can get the list of the queued e-mail as the HTML table. This HTML table lists some of the important information of the e-mails in the scheduling table. These information include:
  • Subject
  • TO address
  • When to be sent
  • State, such as Pending, Re-Try, Canceled or Sent
In addition, you can cancel to send the queued e-mail or update its TO address.
Go Site Map

MailScheduler is Thread Safe:

First of all, some Servlet in your web application needs to initialize the instance of MailScheduler, the main class of this API. Because MailScheduler is Thread Safe, only the single instance can be shared by all the Servlets/JSP in the same web application.

In addition, the back-ground Thread should be started by some Servlet in your web application. This back-ground Thread should be stopped at the time when Tomcat (or any other JSP/Servlet runner) shutdowns. MailScheduler implements the methods for starting/stopping this back-ground Thread. While these methods are wise enough to prevent the back-ground Thread to be started/stopped more than once, we strongly recommend you to start/stop it only once.
All these tasks should be the responsibility of the dummy Servlet. For details about the dummy Servlet, please read:


Go Site Map

Easy programming interface:

In the web application with MailScheduler, the e-mail can be represented by the instance of SmtpMessage. By its setXX( ) and addXX( ) methods, you can set the properties of the e-mail to be sent. For example:


...
// Create the instance of SmtpMessage...
SmtpMessage smtp=new SmtpMessage("example");

// Set the subject...
String subject="Hello";
smtp.setSubject(subject);

// Add the TO address and TO name...
String to_address="foo@bar.com";
String to_name="Foo Bar";
smtp.addToAddress(to_address,to_name);


You can also attache the file to the e-mail to be sent. By the help of OOP MimeParser,
  • The binary contents
  • The file name
  • The Content-Type
of the uploaded file are available in your Servlet. Then, the uploaded file can be attached to the instance of SmtpMessage:

...
// Parse the multipart/form-data of the uploaded file...
// The file larger than 100KB cannot be uploaded!
ParsedData data=mime.parse(req,"ISO-8859-1",100*1024);

// Get the names of FILE INPUTs in the FORM...
String[] keys=data.getFileKeys();

// Iterate through the uploaded files...
for(int i=0; i<keys.length; i++){
    String name=data.getOriginalFileName(keys[i]);
    String type=data.getMimeType(keys[i]);
    byte[] bin=data.getBinaryContents(keys[i]);

    // Attache the uploaded file...
    smtp.attacheFile(bin,name,type);
}


After the instance of SmtpMessage is prepared, you can queue it to the instance of MailScheduler. The scheduled time can be specified along with the Time Zone (TimeZone):

...
// Create the instance of SmtpMessage...
SmtpMessage smtp=new SmtpMessae("example");

// After you set all the properties of SmtpMessage...
String year="2002";
// Month starts with 0!
// So, this means December.
String month="11";
String date="24";
String hour="0";
String minute="0";
// 0 for AM, 1 for PM
String am_pm="0";
String tz="GMT";

// Let's queue the e-mail...
scheduler.queue(smtp,year,month,date,hour,minute,am_pm,tz);


You can get a series of the OPTION tags for the available Time Zones just by:
  • TimeZoneIDs.getTimeZoneOptions(String)
The returned tags can be embed within the SELECTION tag of the FORM.
The scheduled time can also be specified by the long value of the Date object. If you prefer it, please use another version of queue method:
...
// Let's send the e-mail immediately, 
scheduler.queue(smtp,0L);


Anyway, the instance of SmtpMessage will be stored into SQL table through the Object Serialization. At the specified time interval, the back-ground Thread select the e-mails to be sent from the SQL table. Only the e-mails which should be sent at that time will be selected. The selected binary data will be de-serialized back to the instance of SmtpMessage, and the back-ground Thread will send it to the SMPT server. If there is the difficulty establishing the connection to the SMTP server, the back-ground Thread will try to send the e-mail at the next time. You can specify the maximum trial count in the property file, i.e. the back-ground Thread will continue to try sending e-mails upto the specified count.

The above are just the basic functions of MailScheduler. You can also:
  • Cancel to send the specific e-mail
  • Update the TO address
  • Remove the e-mail from the queue
For details, please look into:
Go Site Map

Source Code Example:

Seeing is believing! The very simple example is running on our web site. Please try this example to your heart contents. Then, download and explore its source code.

Requirement:

You need to install:

By the help of OOP SimpleDaoHelper, MailScheduler can get the JDBC Connection in the various way. If the JNDI DataSource is available in your environment, MailScheduler can depend it to query the JDBC Connection. Otherwise, MailScheduler can create the JDBC Connection from the JDBC driver. In any case, you also need to install the JDBC driver for your SQL database. The available JDBC drivers are listed at the URL below: Optionally, MailScheduler can take the full advantage of PooledStatement, another Java API from OOP-Research. This API implements the pooling mechanism for the JDBC PreparedStatement and ensures the Thread Safety of your web application.

As described above, MailScheduler can get the JDBC Connection in the various ways. And the different configuration will be required for your strategy. Please go to the section suitable for your strategy:
Go Site Map

Configuration for JNDI DataSource:

If you specify:

  • com.oopreserch.dao.DaoHelperForJndi
SimpleDaoHelper will look up the JNDI DataSource within your J2EE environment and will ask it the JDBC Connection. To make it possible, the intended JNDI DataSource must be found by:
  • java:comp/env/jdbc/SchedulerDataSource
within your J2EE environment. And the res-auth element of this JNDI DataSource must be Container. Then, SimpleDaoHelper can look up the intended JNDI DataSource and can obtain the JDBC Connection from it.

The docBase directory of your web applications will include the following JAR files and property files:
  • (docBase)/WEB-INF/lib/scheduler2x.jar
  • (docBase)/WEB-INF/lib/oop_util_3_x.jar
  • (docBase)/WEB-INF/lib/daohelper_1_x.jar
  • (docBase)/WEB-INF/lib/activation.jar
  • (docBase)/WEB-INF/lib/mail.jar
  • (docBase)/WEB-INF/classes/MailScheduler.properties
Be sure to give the Read permission for these files.

NOTE for oop_util_3_x.jar:
If you have the older version oop_util_3_x.jar, please replace it with the new one. In other words, please be sure to find only the latest version of oop_util_3_x.jar under WEB-INF/lib directory.

NOTE for JBuilder:
Please add all the jar files above to Required Libraries of your project.

Go Site Map

Configuration for JDBC dirver:

If you specify:

  • com.oopreserch.dao.DaoHelperForJdbc
SimpleDaoHelper will get the JDBC Connection from the specified JDBC driver. To make it possible, the JDBC driver must be available to your web application. Please place the JAR file of your JDBC driver under:
  • (docBase)/WEB-INF/lib
or other directory if several web applications share the same JDBC driver. In addition, you need to edit:
  • (docBase)/WEB-INF/classes/DaoHelperForJdbc.properties
and specify the following properties:
Property nameValue
jdbc_drvThe fully qualified class name of your JDBC driver
jdbc_urlThe URL for the intended database
jdbc_usrThe user name for the specified database
jdbc_pssThe password for the specified database

The template of DaoHelperForJdbc.properties is included in the distribution of SimpleDaoHelper. Based on the properties in this file, MailScheduler can establish the JDBC Connection to your data base and create PreparedStatement from the JDBC Connection.

The docBase directory of your web applications will include the following JAR files and property files:
  • (docBase)/WEB-INF/lib/scheduler2x.jar
  • (docBase)/WEB-INF/lib/oop_util_3_x.jar
  • (docBase)/WEB-INF/lib/daohelper_1_x.jar
  • (docBase)/WEB-INF/lib/activation.jar
  • (docBase)/WEB-INF/lib/mail.jar
  • (docBase)/WEB-INF/lib/(jdbc_driver.jar)
  • (docBase)/WEB-INF/classes/MailScheduler.properties
  • (docBase)/WEB-INF/classes/DaoHelperForJdbc.properties
Be sure to give the Read permission for these files.

NOTE for oop_util_3_x.jar:
If you have the older version oop_util_3_x.jar, please replace it with the new one. In other words, please be sure to find only the latest version of oop_util_3_x.jar under WEB-INF/lib directory.

NOTE for JBuilder:
Please add all the jar files above to Required Libraries of your project.

Go Site Map

Configuration for PooledStatement:

If you specify:

  • com.oopreserch.dao.DaoHelperForPooledStatement
PooledStatement is responsible for all the details, i.e. it establishes the JDBC Connection to your SQL database, creates the JDBC PreparedStatement and keeps it in the pool. The required configuration is described in the README.html of PooledStatement, so please consult it.

The docBase directory of your web applications will include the following JAR files and property files:
  • (docBase)/WEB-INF/lib/scheduler2x.jar
  • (docBase)/WEB-INF/lib/oop_util_3_x.jar
  • (docBase)/WEB-INF/lib/daohelper_1_x.jar
  • (docBase)/WEB-INF/lib/activation.jar
  • (docBase)/WEB-INF/lib/mail.jar
  • (docBase)/WEB-INF/lib/poolps.jar
  • (docBase)/WEB-INF/lib/(jdbc_driver.jar)
  • (docBase)/WEB-INF/classes/MailScheduler.properties
  • (docBase)/WEB-INF/classes/PooledPS.properties
Be sure to give the Read permission for these files.

NOTE for oop_util_3_x.jar:
If you have the older version oop_util_3_x.jar, please replace it with the new one. In other words, please be sure to find only the latest version of oop_util_3_x.jar under WEB-INF/lib directory.

NOTE for JBuilder:
Please add all the jar files above to Required Libraries of your project.

Go Site Map

Download

This API is now a part of Bento framework. There are the better documentations and source code examples on the web site for Bento framework. There, you will also find the link to download the entire framework or each API in it.

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.