Geronimo
  1. Geronimo
  2. GERONIMO-2593

Document Javamail Session creation and usage

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1.1
    • Fix Version/s: 2.1.6, 2.2.1, 3.0.0
    • Component/s: documentation
    • Security Level: public (Regular issues)
    • Labels:
      None
    • Environment:

      WAS CE 1.1.0.1

      Description

      I can't find anywhere in the GDOC11 wiki or WAS CE public documentation that explains how to create a simple system-wide mail Session. The Geronimo console does not have a page to create them and the samples only contain a single "javamail" config project whose plan.xml is incomprehensible to a mere mortal like me.

      Please provide an example how to create a mail Session (or how to get one out of the javamail module configured in config.xml?) and how to reference that session within a webapp.

      If anyone sees this issue and can email me an example, please email mike AT perham.net. I'm stuck on this right now.

      1. sendmail.jpg
        11 kB
        Christopher M. Cardona
      2. sendmail.war
        2 kB
        Christopher M. Cardona

        Activity

        Hide
        Christopher M. Cardona added a comment -

        Hi Mike,

        Hope this helps.

        chris


        Using Geronimo's default JavaMail session:

        Note: This was tested on trunk and should be the same steps for Geronimo v1.2 and v2.0 with minor changes specifically references to JavaMail module ID.

        1. Download Apache JAMES from: http://apache2.openmirrors.org/james/server/binaries/james-2.3.0.zip

        2. Unzip file and run JAMES by executing: \james-2.3.0\bin\run.bat. You should see something like this:

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

        3. Make sure JavaMail module is started:

        • For trunk and v2.0-M1: org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car (by default this is started)
        • For v1.2-beta: org.apache.geronimo.configs/javamail/1.2-beta/car (by default this is not started)
        • To start the module you can:

        a. Use the Web Console: Applications > System Modules > Click 'Start' link beside the JavaMail module ID

        b. Use command line deployer:

        cd <geronimo_home>\bin
        java -jar deployer.jar --user system --password manager start org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car
        

        c. Stop the server. Modify <geronimo_home>\var\config\config.xml and set JavaMail module load attribute to 'true':

         
        ...
        <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>
        ...
        

        Note: 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).

        3. Create a simple webapp (sendmail.war - attached in this issue) containing the following files:

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

        web.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.jsp</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:

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1">
            <environment>
                <moduleId>
                    <groupId>test</groupId>
                    <artifactId>sendmail</artifactId>
                    <version>1.0</version>
                    <type>car</type>
                </moduleId>   
                <dependencies>
                    <dependency>
                        <groupId>org.apache.geronimo.configs</groupId>
                        <artifactId>javamail</artifactId>
                        <version>2.0-SNAPSHOT</version>
                        <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>
        

        index.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>
        

        4. Deploy sendmail.war:

        a. Using Web Console: Applications > Deploy New > Set 'Archive:' to sendmail.war and click 'Install'

        b. Using command line deployer:

        cd <geronimo_home>\bin
        java -jar deployer.jar --user system --password manager deploy sendmail.war
        

        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:

        Result:
        Message sent: Fri Dec 15 01:10:05 PST 2006
        To: manny@pacquiao.com
        From: chris@cardona.com
        
        Show
        Christopher M. Cardona added a comment - Hi Mike, Hope this helps. chris Using Geronimo's default JavaMail session: Note: This was tested on trunk and should be the same steps for Geronimo v1.2 and v2.0 with minor changes specifically references to JavaMail module ID. 1. Download Apache JAMES from: http://apache2.openmirrors.org/james/server/binaries/james-2.3.0.zip 2. Unzip file and run JAMES by executing: \james-2.3.0\bin\run.bat. You should see something like this: Phoenix 4.2 James Mail Server 2.3.0 Remote Manager Service started plain:4555 POP3 Service started plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Disabled 3. Make sure JavaMail module is started: For trunk and v2.0-M1: org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car (by default this is started) For v1.2-beta: org.apache.geronimo.configs/javamail/1.2-beta/car (by default this is not started) To start the module you can: a. Use the Web Console: Applications > System Modules > Click 'Start' link beside the JavaMail module ID b. Use command line deployer: cd <geronimo_home> \bin java -jar deployer.jar --user system --password manager start org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car c. Stop the server. Modify <geronimo_home>\var\config\config.xml and set JavaMail module load attribute to 'true': ... <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> ... Note: 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). 3. Create a simple webapp (sendmail.war - attached in this issue) containing the following files: sendmail.war + index.jsp + WEB-INF + web.xml + geronimo-web.xml web.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.jsp </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: <?xml version= "1.0" encoding= "UTF-8" ?> <web-app xmlns= "http://geronimo.apache.org/xml/ns/j2ee/web-1.1" > <environment> <moduleId> <groupId> test </groupId> <artifactId> sendmail </artifactId> <version> 1.0 </version> <type> car </type> </moduleId> <dependencies> <dependency> <groupId> org.apache.geronimo.configs </groupId> <artifactId> javamail </artifactId> <version> 2.0-SNAPSHOT </version> <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> index.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> 4. Deploy sendmail.war: a. Using Web Console: Applications > Deploy New > Set 'Archive:' to sendmail.war and click 'Install' b. Using command line deployer: cd <geronimo_home> \bin java -jar deployer.jar --user system --password manager deploy sendmail.war 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: Result: Message sent: Fri Dec 15 01:10:05 PST 2006 To: manny@pacquiao.com From: chris@cardona.com
        Hide
        Christopher M. Cardona added a comment -

        Hernan,

        Maybe we can include this in our docs so we have basic documentation for JavaMail. I don't mind doing it but not sure where to put it.

        Thanks,
        chris

        Show
        Christopher M. Cardona added a comment - Hernan, Maybe we can include this in our docs so we have basic documentation for JavaMail. I don't mind doing it but not sure where to put it. Thanks, chris
        Hide
        David Jencks added a comment -

        This suggests a couple of wish-list ideas:

        – a JAMES plugin for geronimo
        – turning this into a testsuite functional test

        Any ideas on how hard it might be to do these?

        thanks
        david jencks

        Show
        David Jencks added a comment - This suggests a couple of wish-list ideas: – a JAMES plugin for geronimo – turning this into a testsuite functional test Any ideas on how hard it might be to do these? thanks david jencks
        Hide
        Chi Runhua added a comment -

        We already document the usage of Java Maill and have it as one of Geronimo sample applications.

        For JAMES plugin, we have https://issues.apache.org/jira/browse/GERONIMO-4117 to track the status.

        For testsuite improvement, I filed a new JIRA as https://issues.apache.org/jira/browse/GERONIMO-5307

        Show
        Chi Runhua added a comment - We already document the usage of Java Maill and have it as one of Geronimo sample applications. For JAMES plugin, we have https://issues.apache.org/jira/browse/GERONIMO-4117 to track the status. For testsuite improvement, I filed a new JIRA as https://issues.apache.org/jira/browse/GERONIMO-5307

          People

          • Assignee:
            Chi Runhua
            Reporter:
            Mike Perham
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development