geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Geronimo v2.1 > sendmail - Using Geronimo's Default JavaMail Session
Date Thu, 03 May 2012 05:21:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=GMOxDOC21&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/GMOxDOC21/sendmail+-+Using+Geronimo%27s+Default+JavaMail+Session">sendmail
- Using Geronimo&#39;s Default JavaMail Session</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~saphen">saphen</a>
    </h4>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >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&#39;t, then Apache JAMES
is a easy one to use for the test.   <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">1.
Download Apache *Java Apache Mail Enterprise System* (JAMES) from: http://james.apache.org/download.cgi
 <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">1.
Download Apache *Java Apache Mail Enterprise System* (JAMES) from: http://james.apache.org/download.cgi,
remember that JAMES support IBM JDK from v3.x. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>2. Unzip/untar file.  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/GMOxDOC21/not-in-svn+-+Inbound+JCA+example"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/GMOxDOC21/not-in-svn+-+Inbound+JCA+example">not-in-svn
- Inbound JCA example</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/GMOxDOC21/Sample+Applications"><img border='0' align='middle'
src='/confluence/images/icons/up_16.gif' width='8' height='8'></a></sup><a
href="/confluence/display/GMOxDOC21/Sample+Applications">Sample Applications</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;<a href="/confluence/display/GMOxDOC21/timereport+-+Web+Application+Security+Sample">timereport
- Web Application Security Sample</a></td><td class='ScrollbarNextIcon'><a
href="/confluence/display/GMOxDOC21/timereport+-+Web+Application+Security+Sample"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>
<p>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.</p>


<h2><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-InstallandRunApacheJAMESifnecessary"></a>Install
and Run Apache JAMES if necessary</h2>

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

<p>1. Download Apache <b>Java Apache Mail Enterprise System</b> (JAMES)
from: <a href="http://james.apache.org/download.cgi" class="external-link" rel="nofollow">http://james.apache.org/download.cgi</a>,
remember that JAMES support IBM JDK from v3.x.</p>

<p>2. Unzip/untar file. </p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-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
</pre>
</div></div>
<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>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.</td></tr></table></div>


<p>4. Make sure Geronimo JavaMail module is started:</p>

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


<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>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).</td></tr></table></div>

<h2><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-TestwithBasicWebApplication"></a>Test
with Basic Web Application</h2>

<p>Create a simple webapp containing the following files:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
sendmail.war
  + index.jsp
  + WEB-INF
    + web.xml
    + geronimo-web.xml
</pre>
</div></div>

<p><b>web.xml:</b></p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>web.xml</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span>
encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
&lt;web-app xmlns=<span class="code-quote">"http://java.sun.com/xml/ns/j2ee"</span>
        <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
        xsi:schemaLocation=<span class="code-quote">"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span>
        version=<span class="code-quote">"2.4"</span>&gt;
    <span class="code-tag">&lt;display-name&gt;</span>Send Mail Webapp<span
class="code-tag">&lt;/display-name&gt;</span>
    <span class="code-tag">&lt;welcome-file-list&gt;</span>
        <span class="code-tag">&lt;welcome-file&gt;</span>index.html<span
class="code-tag">&lt;/welcome-file&gt;</span>
    <span class="code-tag">&lt;/welcome-file-list&gt;</span>
    <span class="code-tag">&lt;resource-ref&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- Used in
index.jsp --&gt;</span></span>
        <span class="code-tag">&lt;res-ref-name&gt;</span>mail/testMailSession<span
class="code-tag">&lt;/res-ref-name&gt;</span>
        <span class="code-tag">&lt;res-type&gt;</span>javax.mail.Session<span
class="code-tag">&lt;/res-type&gt;</span>
        <span class="code-tag">&lt;res-auth&gt;</span>Container<span
class="code-tag">&lt;/res-auth&gt;</span>
        <span class="code-tag">&lt;res-sharing-scope&gt;</span>Shareable<span
class="code-tag">&lt;/res-sharing-scope&gt;</span>
    <span class="code-tag">&lt;/resource-ref&gt;</span>
<span class="code-tag">&lt;/web-app&gt;</span>
</pre>
</div></div>

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

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>geronimo-web.xml</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;?xml version=<span class="code-quote">"1.0"</span>
encoding=<span class="code-quote">"UTF-8"</span>?&gt;</span>
<span class="code-tag">&lt;web-app xmlns=<span class="code-quote">"http://geronimo.apache.org/xml/ns/j2ee/web-1.1"</span>&gt;</span>
    <span class="code-tag">&lt;environment&gt;</span>
        <span class="code-tag">&lt;moduleId&gt;</span>
            <span class="code-tag">&lt;groupId&gt;</span>${pom.groupId}<span
class="code-tag">&lt;/groupId&gt;</span>
            <span class="code-tag">&lt;artifactId&gt;</span>${pom.artifactId}<span
class="code-tag">&lt;/artifactId&gt;</span>
            <span class="code-tag">&lt;version&gt;</span>${version}<span
class="code-tag">&lt;/version&gt;</span>
            <span class="code-tag">&lt;type&gt;</span>war<span class="code-tag">&lt;/type&gt;</span>
        <span class="code-tag">&lt;/moduleId&gt;</span>   
        <span class="code-tag">&lt;dependencies&gt;</span>
            <span class="code-tag">&lt;dependency&gt;</span>
                <span class="code-tag">&lt;groupId&gt;</span>org.apache.geronimo.configs<span
class="code-tag">&lt;/groupId&gt;</span>
                <span class="code-tag">&lt;artifactId&gt;</span>javamail<span
class="code-tag">&lt;/artifactId&gt;</span>
                <span class="code-tag">&lt;type&gt;</span>car<span
class="code-tag">&lt;/type&gt;</span>
            <span class="code-tag">&lt;/dependency&gt;</span>
        <span class="code-tag">&lt;/dependencies&gt;</span>
    <span class="code-tag">&lt;/environment&gt;</span>
    <span class="code-tag">&lt;context-root&gt;</span>/sendmail<span
class="code-tag">&lt;/context-root&gt;</span>
    <span class="code-tag">&lt;resource-ref&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- Used is
web.xml --&gt;</span></span>
        <span class="code-tag">&lt;ref-name&gt;</span>mail/testMailSession<span
class="code-tag">&lt;/ref-name&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- Default
Geronimo mail session --&gt;</span></span>
        <span class="code-tag">&lt;resource-link&gt;</span>mail/MailSession<span
class="code-tag">&lt;/resource-link&gt;</span>
    <span class="code-tag">&lt;/resource-ref&gt;</span>
<span class="code-tag">&lt;/web-app&gt;</span>
</pre>
</div></div>

<p><b>index.jsp:</b></p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>index.jsp</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
&lt;%@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" %&gt;

&lt;%
    String resultMsg = "";
    String action = request.getParameter(<span class="code-quote">"action"</span>);
    if (<span class="code-quote">"Send"</span>.equals(action)) {
        String from = request.getParameter(<span class="code-quote">"from"</span>);
        String to = request.getParameter(<span class="code-quote">"to"</span>);
        String subject = request.getParameter(<span class="code-quote">"subject"</span>);
        String content = request.getParameter(<span class="code-quote">"message"</span>);

        // Get mail session and transport
        InitialContext context = new InitialContext();
        // Mail session from web.xml's resource reference
        Session mailSession = (Session) context.lookup(<span class="code-quote">"java:comp/env/mail/testMailSession"</span>);
        Transport transport = mailSession.getTransport(<span class="code-quote">"smtp"</span>);
    
        // 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 = <span class="code-quote">"<span class="code-tag">&lt;b&gt;</span>Result:<span
class="code-tag">&lt;/b&gt;</span>"</span>;
        resultMsg += <span class="code-quote">"<span class="code-tag">&lt;br&gt;</span>Message
sent: "</span> + new Date();
        resultMsg += <span class="code-quote">"<span class="code-tag">&lt;br&gt;</span>To:
"</span> + to;
        resultMsg += <span class="code-quote">"<span class="code-tag">&lt;br&gt;</span>From:
"</span> + from;
    }
%&gt;

<span class="code-tag">&lt;html&gt;</span>
    <span class="code-tag">&lt;head&gt;</span>
        <span class="code-tag">&lt;title&gt;</span>Send Mail<span class="code-tag">&lt;/title&gt;</span>
    <span class="code-tag">&lt;/head&gt;</span>
    <span class="code-tag">&lt;body&gt;</span>
        <span class="code-tag">&lt;form&gt;</span>
            <span class="code-tag">&lt;table&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"center"</span>
colspan=<span class="code-quote">"2"</span>&gt;</span><span class="code-tag">&lt;b&gt;</span>Send
Mail<span class="code-tag">&lt;/b&gt;</span><span class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"right"</span>&gt;</span>From:<span
class="code-tag">&lt;/td&gt;</span>
                    <span class="code-tag">&lt;td&gt;</span><span class="code-tag">&lt;input
type=<span class="code-quote">"text"</span> name=<span class="code-quote">"from"</span>&gt;</span><span
class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"right"</span>&gt;</span>To:<span
class="code-tag">&lt;/td&gt;</span>
                    <span class="code-tag">&lt;td&gt;</span><span class="code-tag">&lt;input
type=<span class="code-quote">"text"</span> name=<span class="code-quote">"to"</span>&gt;</span><span
class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"right"</span>&gt;</span>Subject:<span
class="code-tag">&lt;/td&gt;</span>
                    <span class="code-tag">&lt;td&gt;</span><span class="code-tag">&lt;input
type=<span class="code-quote">"text"</span> name=<span class="code-quote">"subject"</span>&gt;</span><span
class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"right"</span>&gt;</span>Message:<span
class="code-tag">&lt;/td&gt;</span>
                    <span class="code-tag">&lt;td&gt;</span><span class="code-tag">&lt;textarea
rows=<span class="code-quote">"5"</span> cols=<span class="code-quote">"20"</span>
name=<span class="code-quote">"message"</span>&gt;</span><span class="code-tag">&lt;/textarea&gt;</span><span
class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
                <span class="code-tag">&lt;tr&gt;</span>
                    <span class="code-tag">&lt;td align=<span class="code-quote">"right"</span>
colspan=<span class="code-quote">"2"</span>&gt;</span>
                        <span class="code-tag">&lt;input type=<span class="code-quote">"submit"</span>
name=<span class="code-quote">"action"</span> value=<span class="code-quote">"Send"</span>&gt;</span>&amp;nbsp;<span
class="code-tag">&lt;input type=<span class="code-quote">"reset"</span>&gt;</span><span
class="code-tag">&lt;/td&gt;</span>
                    <span class="code-tag">&lt;/td&gt;</span>
                <span class="code-tag">&lt;/tr&gt;</span>
            <span class="code-tag">&lt;/table&gt;</span>
        <span class="code-tag">&lt;/form&gt;</span>
        <span class="code-tag">&lt;%= resultMsg %&gt;</span>
    <span class="code-tag">&lt;/body&gt;</span>
<span class="code-tag">&lt;/html&gt;</span>
</pre>
</div></div>

<h2><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-BuildingandDeployingtheWebApplication"></a>Building
and Deploying the Web Application</h2>
<p>Download the sendmail application from the following link:<br/>
<a href="/confluence/download/attachments/78717/sendmail_.zip?version=1&amp;modificationDate=1204740518000">sendmail</a></p>

<p>After decompressing the given file, the sendmail directory will be created.</p>


<h2><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-SourceCodeforSample"></a>Source
Code for Sample</h2>

<p>Please reference <a href="/confluence/display/GMOxDOC21/Sample+Applications" title="Sample
Applications">Samples General Information</a> for information on obtaining and building
the source for this and other samples</p>


<h3><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-BuildtheWebApplication"></a>Build
the Web Application</h3>
<p>The <b>sendmail</b> folder will already contain an ear file ready to
be deployed. However, you can still play with the source and build it yourself.</p>

<p>Use a command prompt to navigate into the <b>sendmail</b> directory and
just give <b>mvn install site</b> command to build. It will overwrite the <b>sendmail-ear-2.0-SNAPSHOT.ear</b>
under the <b>sendmail</b> 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.</p>

<h3><a name="sendmail-UsingGeronimo%27sDefaultJavaMailSession-DeploytheWebApplication"></a>Deploy
the Web Application</h3>
<ol>
	<li>Navigate to <b>Deploy New</b> from the Console Navigation panel.</li>
	<li>Load <b>sendmail-ear-2.0-SNAPSHOT.ear</b> from <b>sendmail</b>
folder in to the <b>Archive</b> input box.</li>
	<li>Press <b>Install</b> button to deploy application in the server.</li>
</ol>


<p>5. Test send mail webapp by going to: <a href="http://localhost:8080/sendmail"
class="external-link" rel="nofollow">http://localhost:8080/sendmail</a></p>

<p>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:</p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeContent
panelContent">
<pre class="code-xml">
Result:
Message sent: Fri Dec 15 01:10:05 PST 2006
To: manny@pacquiao.com
From: chris@cardona.com
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/GMOxDOC21/sendmail+-+Using+Geronimo%27s+Default+JavaMail+Session">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=78717&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/GMOxDOC21/sendmail+-+Using+Geronimo%27s+Default+JavaMail+Session?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message