This article describes how to install a typical mail system, and access it from a simple web application to provide a server based email client. It does not go into details about how to create a configure the mail system itself.

Install and Run Apache JAMES if necessary

This sample requires that you have an smtp server running. The real value in the sample is to demonstrate how to configure the mail session and connect to it to send a message. If you already have an smtp server configured and running then you can skip this step. However, if you don't, then Apache JAMES is a easy one to use for the test.

1. Download Apache Java Apache Mail Enterprise System (JAMES) from: http://james.apache.org/download.cgi

2. Unzip/untar file.

3. Run JAMES by executing: \james-2.3.1\bin\run.bat. You should see something like this:

xml Phoenix 4.2 James Mail Server 2.3.1 Remote Manager Service started plain:4555 POP3 Service started plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Disabled

For MAC OSX and possibly other unix based systems James must be run under the super user to have authorization to leverage the designated ports.

4. Make sure Geronimo JavaMail module is started:

  • Check Geronimo console System Modules for a component name beginning with org.apache.geronimo.configs/javamail. This module is present and started by default in the javaee5 instances of the server (both jetty and tomcat).
  • If, for some reason, the module is not started or not present then you can start the module via:
    • Use the Web Console: Applications > System Modules > Click 'Start' link beside the JavaMail module ID (you may have to enable expert mode)
    • Use command line deployer: xmlsolid cd <geronimo_home>\bin java -jar deployer.jar --user system --password manager start org.apache.geronimo.configs/javamail/2.X/car
    • Stop the server. Modify <geronimo_home>\var\config\config.xml and set JavaMail module load attribute to 'true': xmlsolid ... <module load="true" name="org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car"> <gbean name="SMTPTransport"> <attribute name="host">localhost</attribute> <attribute name="port">25</attribute> </gbean> </module> ...

You can configure the SMTP transport's host and port by overriding the values of the SMTPTransport GBean attrbitues. For our test we will use the default. This will allow us to connect to JAMES running in localhost and using port 25 (the default SMTP port).

Test with Basic Web Application

Create a simple webapp containing the following files:

xml sendmail.war + index.jsp + WEB-INF + web.xml + geronimo-web.xml


xmlsolidweb.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Send Mail Webapp</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <resource-ref> <!-- Used in index.jsp --> <res-ref-name>mail/testMailSession</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> </web-app>

geronimo-web.xml: This deployable is using Geronimo 2.0's JavaMail component, so it needs to specify where to look. It does this under the <dependencies> tag. As Geronimo changes, these paths need to be changed accordingly to ensure that it refers to the same things. Under the <resource-ref> element, it is referring to a JavaMail session. This is what allows the developer to reference resources that resides on the server. The <ref-name> element is used in the web.xml so the two must be consistent. The <resource-link> element specifies the default Geronimo mail session.

xmlsolidgeronimo-web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1"> <environment> <moduleId> <groupId>${pom.groupId}</groupId> <artifactId>${pom.artifactId}</artifactId> <version>${version}</version> <type>war</type> </moduleId> <dependencies> <dependency> <groupId>org.apache.geronimo.configs</groupId> <artifactId>javamail</artifactId> <type>car</type> </dependency> </dependencies> </environment> <context-root>/sendmail</context-root> <resource-ref> <!-- Used is web.xml --> <ref-name>mail/testMailSession</ref-name> <!-- Default Geronimo mail session --> <resource-link>mail/MailSession</resource-link> </resource-ref> </web-app>


xmlsolidindex.jsp <%@page import="java.util.Date, javax.mail.Message, javax.mail.Session, javax.mail.Transport, javax.mail.internet.InternetAddress, javax.mail.internet.MimeMessage, javax.naming.InitialContext" %> <% String resultMsg = ""; String action = request.getParameter("action"); if ("Send".equals(action)) { String from = request.getParameter("from"); String to = request.getParameter("to"); String subject = request.getParameter("subject"); String content = request.getParameter("message"); // Get mail session and transport InitialContext context = new InitialContext(); // Mail session from web.xml's resource reference Session mailSession = (Session) context.lookup("java:comp/env/mail/testMailSession"); Transport transport = mailSession.getTransport("smtp"); // Setup message MimeMessage message = new MimeMessage(mailSession); // From address message.setFrom(new InternetAddress(from)); // To address message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Subject message.setSubject(subject); // Content message.setText(content); // Send message transport.connect(); transport.send(message); // Build result message resultMsg = "<b>Result:</b>"; resultMsg += "<br>Message sent: " + new Date(); resultMsg += "<br>To: " + to; resultMsg += "<br>From: " + from; } %> <html> <head> <title>Send Mail</title> </head> <body> <form> <table> <tr> <td align="center" colspan="2"><b>Send Mail</b></td> </tr> <tr> <td align="right">From:</td> <td><input type="text" name="from"></td> </tr> <tr> <td align="right">To:</td> <td><input type="text" name="to"></td> </tr> <tr> <td align="right">Subject:</td> <td><input type="text" name="subject"></td> </tr> <tr> <td align="right">Message:</td> <td><textarea rows="5" cols="20" name="message"></textarea></td> </tr> <tr> <td align="right" colspan="2"> <input type="submit" name="action" value="Send">&nbsp;<input type="reset"></td> </td> </tr> </table> </form> <%= resultMsg %> </body> </html>

Building and Deploying the Web Application

Download the sendmail application from the following link:

After decompressing the given file, the sendmail directory will be created.

Source Code for Sample

Please reference Samples General Information for information on obtaining and building the source for this and other samples

Build the Web Application

The sendmail folder will already contain an ear file ready to be deployed. However, you can still play with the source and build it yourself.

Use a command prompt to navigate into the sendmail directory and just give mvn install site command to build. It will overwrite the sendmail-ear-2.0-SNAPSHOT.ear under the sendmail folder. Any dependency on specific versions of javamail may be edited in the geronimo-web.xml deployment plan in sendmail-war/src/main.webapp/WEB-INF/geronimo-web.xml.

Deploy the Web Application

  1. Navigate to Deploy New from the Console Navigation panel.
  2. Load sendmail-ear-2.0-SNAPSHOT.ear from sendmail folder in to the Archive input box.
  3. Press Install button to deploy application in the server.

5. Test send mail webapp by going to: http://localhost:8080/sendmail

Fill up the form (From, To, Subject, Message fields) and click 'Send' button. You should get a similar message if the mail was sent successfully:

xmlsolid Result: Message sent: Fri Dec 15 01:10:05 PST 2006 To: manny@pacquiao.com From: chris@cardona.com
  • No labels