OOP-ResearchMake It Simpler by Object Oriented Programming

How to send e-mails by Servlet/JSP with JavaMail API / File Upload Servlet/JSP by multipart/form-data / Select Time Zone (TimeZone) / Object Serialization

Free source code example for how to send e-mail by Servlet with JavaMail API. You can upload files by multipart/form-data and attache them to the e-mail. You can send e-mails at the scheduled time. When you specify when the e-mail will be sent, the available TimeZones will be listed and you can select the favorite Time Zone among them. The e-mail is stored into the SQL table by the Object Serialization. Once the e-mail is queued, the back-ground Thread will select it at the scheduled time and send it.

When you edit the new e-mail, all the fields on the CGI form must be filled. To check the availability of these fields, this example JSP/Servlet uses FormGenerator. By the help of this Java API, a few lines of code are enough to check the validities of all the fields on the CGI form.

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.

Download the source code example:

Related Pages:


About this free source code example:

This free source code example shows how to send the e-mail at the scheduled time from Servlets/JSP with JavaMail API. It also includes the file upload Servlet/JSP by multipart/form-data. The binary contents of the uploaded file will be attached to the e-mail along with the file name and the Content-Type. You can specify when the e-mail will be sent by your favorite Time Zone (Java TimeZone object).
By the easy programming interface of OOP MailScheduler, a few lines of code are enough to:

  • Set the properties (such as TO address, subject and so on) of the e-mail to be sent
  • Attache the binary contents of the uploaded file
  • Save the e-mail into the SQL table by Object Serialization
  • List the available Time Zones (Java TimeZone objects)
  • Specify when the e-mail will be sent by the selected Time Zone (Java TimeZone object)
Then, the back-ground Thread will select the queued e-mail at the appropriate time and send it to the SMTP server through JavaMail API. That's all.
OOP MimeParser is responsible for parsing the multipart/form-data. After it parses the InputStream of the multipart/form-data of the uploaded files,
  • The binary contents of the uploaded file
  • The name of the uploaded file
  • The Content-Type of the uploaded file
are available at the destination Servlet/JSP. And you can attache the uploaded file to the e-mail. You can specify the maximum size of the file to be uploaded. If the larger file is uploaded, this API throws the exception. In this case, the example Servlet/JSP can catch the exception and show the appropriate message.

In addition, OOP MimeParser can restrict the mime-types (or Content-Types) of the file to be uploaded. If the user tries to upload the file with another mime-type, OOP MimeParser throws the exception. Again, in this case, the example Servlet/JSP can catch the exception and show the appropriate message.

When the user edit the new e-mail, s/he should fill all the fields on the CGI form. To check the availabilities of all the required data, the example Servlet/JSP uses OOP FormGenerator. By the help of this Java API, a few lines of code are enough to check the validities of the data from the CGI form.

Go Site Map

Save the uploaded file on the file system:

You may want to save the uploaded file on the file system, rather than getting its binary contents directly. Don't worry. OOP MimeParser also implements the methods for this case. This API parses the InputStream of the multipart/form-data and saves the uploaded file under the specific directory. If you have an interest in this feature, please look into another source code example.

Go Site Map

How to start the back-ground Thread:

The queued e-mail will be selected by the back-ground Thread at its scheduled time and will be sent to the SMTP server. You may wonder how such a Thread can be started/stoped from your Servlet/JSP.
Don't worry. OOP MailScheduler implements the methods for these tasks. While these methods are wise enough to prevent the back-ground Thread to be started/stopped more than once, we strongly recommend you to implement some kind of dummy Servlet. For details about the dummy Servlet, please read:

Go Site Map

Download example source code

The source code of this example is available at the link below:

This example depends on the following Java APIs: These Java APIs are available at Java API Shop by OOP-Research. Once you entered in this online shop, you can add as many items as you like to your shopping cart. To download the 30 days FREE trial version, please click Try button for the intended API.
Optionally, you can use: along with OOP MailScheduler. OOP PooledStatement implements the pooling mechanism for the PreparedStatement and ensures the Thread Safety of your web application.
Go Site Map Go Shop

Installation

In addition, OOP MailScheduler requires the following APIs:

For details about the installation of OOP MailScheduler, please read README.html in its distribution.

In this distribution, all the source files are pre-compiled into:
  • scheduler_1/WEB-INF/classes/
So, you can try this example without compiling the source again. After the source code of this example extracted, please place all the required JAR files and property files. As a result, the scheduler_1 directory will include the following JAR files and property file:
  • scheduler_1/WEB-INF/lib/scheduler2x.jar
  • scheduler_1/WEB-INF/lib/mime.jar
  • scheduler_1/WEB-INF/lib/daohelper_1_x.jar
  • scheduler_1/WEB-INF/lib/formgen.jar
  • scheduler_1/WEB-INF/lib/oop_util_3_x.jar
  • scheduler_1/WEB-INF/lib/activation.jar
  • scheduler_1/WEB-INF/lib/mail.jar
  • scheduler_1/WEB-INF/lib/(poolps.jar)
  • scheduler_1/WEB-INF/lib/(jdbc_driver.jar)
  • scheduler_1/WEB-INF/classes/MailScheduler.properties
  • scheduler_1/WEB-INF/classes/MimeParser.properties
  • scheduler_1/WEB-INF/classes/AcceptMime.properties
  • scheduler_1/WEB-INF/classes/FormGenerator.properties
  • scheduler_1/WEB-INF/classes/FormValueBreakerFactory.properties
  • scheduler_1/WEB-INF/classes/FormValueEscaperFactory.properties
  • scheduler_1/WEB-INF/classes/FormValueVerifierFactory.properties
  • scheduler_1/WEB-INF/classes/(DaoHelperForJdbc.properties)
  • scheduler_1/WEB-INF/classes/(PooledPS.properties)
Be sure to give the Read permission for these files.

Because the installation of OOP MailScheduler depends on how it gets the JDBC Connection, the above file structure may differ with your strategy. For example,
  • scheduler_1/WEB-INF/classes/DaoHelperForJdbc.properties
is required only if you use the JDBC driver with OOP MailScheduler. For details, please consult README.html in the distribution of OOP MailScheduler.

Only if you use OOP MailScheduler along with OOP PooledStatement, you need to edit:
  • scheduler_1/WEB-INF/web.xml
Please replace the following elements:


    <servlet>
        <servlet-name>
	InitAndDestroy
        </servlet-name>
        <servlet-class>
	example.scheduler1.InitAndDestroy
        </servlet-class>
	<load-on-startup>
	1
	</load-on-startup>
    </servlet>



with:


    <servlet>
        <servlet-name>
	InitAndDestroy
        </servlet-name>
        <servlet-class>
	example.scheduler1.InitAndDestroyWithPool
        </servlet-class>
	<load-on-startup>
	1
	</load-on-startup>
    </servlet>



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

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.