Create email-based apps with JAMES
Tutorial Details:
Create email-based apps with JAMES
Create email-based apps with JAMES
By: By Serge Knystautas
The open source JAMES mail server offers the tools to develop email-based applications in Java
hen it's not ideal to send users to a Website or to launch a GUI-based application, email-based applications can offer an innovative way for users to communicate with server software. Developers have long recognized the value of sending generated emails to users, but until now they have been unable to listen in response. Traditionally, the developer's best approach for a response from generated email was to have the user click a link in the email, sending him or her to a Website URL.
With an email-based application, a user can send an email to a server; the server can then process that email, making the communication two-way. Email-based interaction offers advantages over sending a user to a Website. First, interaction proceeds even if the user is offline. A user can read her email and, in turn, reply with a series of emails that create an instruction queue for a remote system. Second, under some circumstances a user can respond quicker via an email. If the needed response is simple, an email proves faster than Website interactions because the user stays within her email software.
The Java Apache Mail Enterprise Server (JAMES), an open source effort from the Jakarta Project, allows for custom email processing. With JAMES, developers can process messages from users via email. JAMES offers POP3 (Post Office Protocol) and NNTP (Network News Transport Protocol) support, and may support IMAP (Internet Message Access Protocol) in a future release.
In this article, I present an online scheduling system that uses JAMES to capture users' availability. In the future, the scheduling system may use JAMES to track the validity of email addresses, track unsuccessfully delivered scheduling requests, and otherwise further integrate the Web and email sides of the system.
This article assumes that you know how to send an email using JavaMail. (For a JavaWorld article on JavaMail, read " JavaMail Quick Start .") I focus on how to design the communication flow for an email-based application, how to set up JAMES, and how to process user messages.
Note: You can download this article's source code from Resources .
Plan the interaction
In our scheduling system, buyers look for sellers available to do a small piece of work. In the existing system, buyers use a Website to enter a request that is sent to a group of sellers, generating an email to each seller about the request. Sellers receive the email, click an HTTP link to launch a browser, and view the requests on the Website. Sellers then use the Website to enter their availability status and optional notes about why they are or are not available.
In the new approach, the seller composes an email in reply to the email the scheduling system generates. For the seller, there are only two possible responses: he is either available or unavailable. To make his decision known, the seller will send his reply to one of two special email addresses; depending on the address to which the seller responds, his availability will be apparent.
These special email addresses contain the seller's state and a unique identifier for the request in question. The address's structure looks like this:
request-[availability]-[request ID]@mydomain.com
For instance, a seller reading about request 79834 who wants to indicate his availability would send a message to:
request-available-79834@mydomain.com
When the server receives the seller's email, it can identify the request and availability status from the target address, and identify the seller by way of his email address. If this information is sensitive, you could include a hash parameter based on the seller's account and the request ID instead of the plain request number. The encoded address would then look like this:
request-available-79834-a87bc8e0d0ea8@mydomain.com
The hash parameter a87bc8e0d0ea8 confirms that the message was not from a seller indicating availability for someone else.
Note that the seller need not type the special address; the email inserts it for both available and unavailable responses in the header and message body. The email contains the special email address that indicates availability in the Reply-To header line. Usually, when the seller hits the Reply button on his email client, that client sends a message addressed to the special email address. The unavailability-indication address resides in the CC header line. When the seller hits the Reply to All button on his email client, the unavailability-indication address appears in the list of recipients. The seller can then delete the other address that indicates availability.
The email's body contains the special email addresses in the instructions; most email software will make the address clickable. Also, if you send HTML email, you can use mailto: links easily generate messages to the appropriate address.
The Mailet API
Now that you know what you are listening for, you need to write code that will do the listening. The Mailet API makes the new approach possible. The API builds on the JavaMail API and uses an approach similar to the servlet specification's lifecycle methods and object hierarchy.
The Mailet API comprises two core classes: Matcher and Mailet . Both are interfaces with abstract implementations available as GenericMatcher and GenericMailet , respectively. A matcher determines whether a message should be processed, while a mailet processes the message. A mailet container, which is a mailet-compliant SMTP (Simple Mail Transfer Protocol) mail server, instantiates and manages both.
The matcher determines the routing inside a mailet container. The match(Mail mail) method returns a Collection of recipients that meet the matcher's criteria. (The result is a Collection because a message can contain multiple recipient addresses during SMTP transport.)
You'll also find an abstract GenericRecipientMatcher implementation that helps the matching based on the recipient's address. It iterates through the recipient in the Collection and generates the resulting Collection object so the developer need only implement the matchRecipient(MailAddress recipient) method.
The mailet defines methods to initialize a mailet, service a message, and remove a mailet from the server. The mailet container calls these life-cycle methods in the following sequence:
The mailet is constructed, then initialized with the init(MailetConfig config) method
Any messages for the service(Mail mail) method are handled
The mailet is taken out of service, destroyed with the destroy() method, then garbage collected and finalized
MailetConfig , analogous to the Servlet API's ServletConfig , allows the mailet container to pass initialization parameters to the mailet.
Other useful Mailet API classes and interfaces include Mail , MailAddress , and MailetContext . The Mail interface wraps a MimeMessage object with SMTP delivery information, including the sender's address, a recipient's Collection , the connecting machine's remote IP address, and the message state in the mailet container. MailAddress , a more rigorous implementation of JavaMail's InternetAddress , provides helper methods to find an email address's domain and user part. The MailetContext is analogous to the ServletContext as it provides access to the mailet container.
For the scheduling system, you will build a simple matcher that checks for addresses matching the address specification discussed above, and a mailet to process the responses and determine the sellers' availability status.
Install and configure JAMES
You can download the latest version of JAMES from the Jakarta Project Website. At press time, the most recent version was 2.0 alpha 1 -- a stable release with significant speed and feature improvements over version 1.2.1. It comprises near-production quality code, but the project elected to release an alpha version to allow for the option to change the configuration file's structure before the final release.
After downloading and extracting the distribution to your hard drive, start JAMES by running run.bat or run.sh in the bin directory. JAMES will extract itself in the apps directory, and the basic service will begin running.
To configure JAMES, you need to modify the configuration file ( apps/james/conf/config.xml ). If you run JAMES on a machine without a running local DNS server, you need to identify one in the config file; at line 278, add another line with an available DNS server's IP address. You need to specify the domain names JAMES should handle. At line 37, add any additional domain names your machine should handle, according to your host settings.
You also need to configure a database connection. In the database connection section at line 602, either uncomment the appropriate data source or create your own by specifying the appropriate , (the JDBC [Java Database Connectivity] URL), , and . Keep the name of the data source as maildb .
The custom matcher
The matcher, dubbed SchedulingRequestMatcher , extends GenericRecipientMatcher to match based on the recipient's address. The matcher will not attempt to validate the address's structure, only determine whether the address starts with request-available- or request-unavailable- . The single method looks like this:
public boolean matchRecipient(MailAddress recipient) {
if (recipient.getUser().startsWith("request-available-")) {
return true;
}
if (recipient.getUser().startsWith("request-unavailable-")) {
return true;
}
return false;
}
As mentioned above, you could validate the address in the matcher; instead, we'll leave it to the mailet to determine the validity. While it is unlikely that this kind of address would have errors, it's better to have it match and then let the mailet catch any errors and reply with error mess
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Create email-based apps with JAMES
View Tutorial: Create email-based apps with JAMES
Related
Tutorials:
|
Displaying 1 - 50 of about 1750 Related Tutorials.
|
Clear Data Builder
create a complete Flex-Java-Database apps in minutes. Here is how it works: you... to Clear Data Builder. No need to write and debug Java code, no need to create O/R |
Online Marketing and Email Campaign
programs.
Develop email based e-marketing campaign schedule . ...
Online Marketing and Email Campaign
Online Marketing and Email Campaign
  |
Create a database
.
CREATE TABLE email
(
LName varchar(20),
FName varchar(20...
Create Database in SQL,SQL Tutorial
Create...;
In this section, we will teach you to create a database |
Create a ToolBar
Create a ToolBar
Create a ToolBar
 ...;
This section illustrates you how to create a ToolBar in SWT.
To create |
Create File in Java
Create File in Java,How to Create File in Java,Create File Using Java Code
Create a File
 ... and lines etc.
In this section, we will see
how to create a file. This example |
How to create Link in SWT
How to create Link in SWT
How to create Link...;
In this section, you will study how to create Link in a text.
SWT allows to create link by using the class Link of package |
Create a JSpinner Component in Java
Java Spinner,Create JSpinner Component in Java,Jspinner Example in Java
Create a JSpinner Component in Java
 ...;
In this section, you will learn how to create a
JSpinner component of swing |
Create Shape in Excel Using JSP
How we can create shape in excel using jsp
How we can create shape in excel using jsp
create shape in excel using jsp ...;
In this program we are going create a sheet  |
Create - XML File (Document)
Java Create XML File,Java Create XML Document,Create XML Java,Create XML Files
Create - XML File (Document)
 ...;
In this section, you will learn to create a XML
document using the DOM APIs. This XML |
Create a Frame in Java
AWT Frame,Java AWT Frame,How to Make AWT Frame,Create AWT Frame in Java
Create a Frame in Java
 ...
This program shows you how to create a frame in java AWT package. The frame in java |
Create a Container in Java awt
Container in Java,Create Container in Java,AWT Container Program,Java AWT Container Tutorials
Create a Container in Java awt...;
Introduction
This program illustrates you how to create |
Create a JList Component in Java
Create a JList Component in Java
 ..., you will learn how to create
a JList component of swing. JList... which is used to create a
list which contains items. This class extends |
Create JTree using an Object
Create Jtree in Java,Creating Tree Using an Object,How to Create Tree Using Object in Java Swing
Create JTree using an Object...;
In this section you
will learn to create a JTree using |
Create a JRadioButton Component in Java
Swing
Create a JRadioButton Component in Java...;
In this section, you will learn how to create a radio
button in java... of the javax.swing.*;
package, which is used to create a group of radio buttons from which you |
Create an Empty DOM Document
Create an Empty DOM Document, XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials
Create an Empty DOM Document
 ...;
This Example shows how to create an empty DOM Document
. JAXP (Java API for XML |
Create Scale in SWT
Create Scale in SWT
Create Scale in SWT...;
In this section, you will study how to create scale.
In order to create the scale, SWT provides the class Scale of package |
create Multiple Lines
create Multiple Lines
Create Multiple
Lines
 ... to create a colorful line. You
can set thickness and y position of the text |
How to Create CurveDraw In Java
Create CurveDraw in Java,Create Curve Draw Example Java,How to Draw Curve in Java Using Awt
How to Create CurveDraw In Java...;
Introduction
In this section, you will learn how to create CurveDraw |
Create Radio Buttons in SWT
Create Radio Buttons in SWT
Create Radio...;
This section illustrates you how to create radio button.
In SWT, the style RADIO defined in the Button class allows to create |
How to Create Button on Frame
How to Create Button on Frame,Create a Button in Java,Code to Create Button in Java
How to Create Button on Frame...;
In this section, you will learn how to create Button
on frame the topic |
How to create CheckBox On frame
Create CheckBox on Frame,How to Create Checkbox Component on Frame,Java Checkbox Program Code
How to create CheckBox On frame... how to create CheckBox on the frame. In the Java AWT, top-level
windows |
Create a ToolBar in Java
JToolbar Example,Java Toolbar,Create Toolbar Icons Java,How to Create Tool Bar in Java
Create a ToolBar in Java...;
In this section, you will learn how to create toolbar in java. Swing |
Create PowerPoint Slide Using Java
Create PowerPoint Slide
Using Java
Create PowerPoint... to create a PowerPoint slide .
HWSL is used to make the Microsoft PowerPoint 97...,true)
is used to get the text from both.
To create a slide show we need  |
How to create Combo Box in SWT
How to create Combo Box in SWT
How to create Combo Box in SWT
 ...;
This section illustrates you how to create |
How to create Animation
How to create Animation
How to create Animation
 ...;
This section illustrates you how to create Animation. We |
Create Menu Bar in SWT
Create Menu Bar in SWT
Create Menu Bar in SWT...;
This section illustrates you how to create a menu bar... is
allowed to create a menu bar. The method setMenu() sets the pull down
menu |
Create Popup List in SWT
Create Popup List in SWT
Create Popup List...;
In this section, you will learn how to create the popup list.
SWT allows to create the popup list by providing the class PopupList |
Create Web Page with jsp
Create your first Tomcat Web application with jsp
Web... to create first web
page on tomcat
server. JSP simply application that work... date and time on client browser.
For this we will create a simple dynamic JSP page |
Create Progress Bar in SWT
Create Progress Bar in SWT
Create Progress Bar...;
This section illustrates you how to create a Progress Bar.
SWT allows to create a Progress bar by providing the class ProgressBar |
Create Excel Sheet Using JSP
How we create excel sheet using jsp
create excel...;
In this program, we are going to create the cells and
rows into excel sheet using
java .You can create any number of cells |
How to Create Circle In Java
How to Create Circle in Java,Create Circle Using Java Awt,Create Circle Example in Java
How to Create Circle In Java... will learn how to create Circle Diagram. The java circle is the most
fundamental |
How to Create JSP Page
How to Create JSP Page
How to Create JSP Page...;
In this section we will show you how you can create JSP page... can be
used.
In this example I will show you how to create a simple JSP page |
Create a Sash in SWT
Create a Sash in SWT
Create a Sash in SWT...;
In this section, you will study how to create a Sash
In SWT... to drag the outline. We have create the Sash first, so that the
controls can |
Create dynamic page through JSP
How to read text file in Servlets
Create dynamic page... that shows how to create
dynamic page. This code shows one by one record of student..., application fetch next
record from database.
Create a database :
Before running |
Create a Scroll Pane Container in Java
Example Java
Create a Scroll Pane Container in Java...;
In this section, you will learn how to create a scroll
pane container in Java Swing. When you simply create a Text Area and putting
text |
How to create LineDraw In Java
LineDraw in Java,Create Line Draw in Java,How to Create Line Draw in Java
How to create LineDraw In Java
 ... to create Line Drawing. This program implements a line Drawing
component. A java |
Create Excel Sheet Using JSP
How we create excel sheet using jsp
create excel sheet using jsp
 ...;
In this example we create excel sheet |
Create a Desktop Pane Container in Java
Desktop Container Java,Create Desktop Pane Container,Example Code for Desktop Pane Container
Create a Desktop Pane Container...;
In this section, you will learn how to create a desktop |
Create and Save Excel File in JSP
Create Excel File in JSP,How to Create and Save Excel Spreadsheet File in JSP
Create and Save Excel File in JSP... application.
In this example we are going to create a new excel sheet |
Create Tabs in Java using SWT
Create Tabs in SWT
Create Tabs in Java using...;
This Tab Example in Java, will teach you how to create tabs using SWT in
Java. After going through the example you will be able to create |
Java Create Directory - Java Tutorial
Java Create Directory,Directory Creation in Java,How to Create Directory in Java
Java Create Directory - Java Tutorial...: dir1/dir2/dir3 created
C:\nisha>
This program takes inputs to create |
How to Create Text Area In Java
Create Textarea in Java,JText Area Example,How to Create Text Area in Java
How to Create Text Area In Java
 ...;
In this section, you will learn how to create Text Area
in Java. This section |
Ant Script to Create Mysql Table
Ant Script to Create Mysql Table
Ant Script to Create Mysql Table
 ...;
This example illustrates how to create |
Create a Custom Log Level in Java
Create a Custom Log Level in Java
Create a Custom Log Level in Java
Create a Custom Log Level in Java
 ...;
This section tells you how to create a custom log
level that means log levels are created |
Java Create Directory - Java Tutorial
Java Create Directory - Java Tutorial
Java Create Directory - Java Tutorial
 ... you will learn how to
create directory using java program. This program also |
Create Layout Components in a Grid in Java
Create Layout Components in a Grid,How to Create Layout in Grid Using Java Swing
Create Layout Components in a Grid in Java...;
In this section, you will learn how to create layout components |
Create Data Grid in JSP page
Create Data Grid in JSP page
Create Data Grid in JSP...;
This is detailed code that how to create data... below we have
used a tag library that provide some tags to create data grid |
Create a polar chart using JFreeChart
Create a polar chart using JFreeChart
Create... to create a polar chart using JFreeChart. Code given below creates a simple
polar... ApplicationFrame to create a frame and also pass a string value to the constructor |
Create a Custom Cell Renderer in a JTable
Custom Cell Renderer,Java Table Custom Renderer,Table Cell Renderer,Create a Custom Cell Renderer in a JTable
Create a Custom... to create a custom cell
renderer in a JTable component. Here, first of all you will know |
How to Create Excel Page Using JSP
How to create excel page using jsp
How to create excel page using jsp
how to create excel page using jsp...;
In this program, we are going to create an excel using
java .By going |
|
|
|