HomeDocumentation > 9. サンプル・アプリケーション > 9.e. Geronimo のデフォルト JavaMail セッションの利用
{scrollbar}

Trunk (訳注: 開発中の状態のもの) でテストしました。Geronimo v1.2 から v2.0 への JavaMail のモジュール ID 参照定義のマイナーな変更をした上で v1.2 のサンプルと同じステップとなります。

Apache JAMES の導入

1. http://apache2.openmirrors.org/james/server/binaries/james-2.3.0.zip から Apache JAMES をダウンロードしてください。

2. ファイルを解凍し、\james-2.3.0\bin\run.bat コマンドで JAMES を実行してください。以下のように表示されます。

xml 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. 次の JavaMail モジュールが始動していることを確認してください。

  • 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:
  • Trunk か v2.0-M1 の場合: org.apache.geronimo.configs/javamail/2.0-SNAPSHOT/car (デフォルトでは始動しています)
  • v1.2-beta の場合: org.apache.geronimo.configs/javamail/1.2-beta/car (デフォルトでは始動していません)
  • このモジュールを始動するには、次のいずれかの方法があります

a. コンソールを使って Application > System Modules > JavaMail モジュール ID の横にある 'Start' をクリックします
b. コマンドライン・デプロイヤーを利用して、以下のコマンドを入力します

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

c. サーバーを停止し、<geronimo_home>\var\config\config.xml を編集し、以下のとおり JavaMail モジュールの load 属性を '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> ...

SMTP Transport GBean の値を上書きすることで、SMTP トランスポートのホストや通信ポートを構成することができます。このサンプルはデフォルトの設定を利用してテストしました。こうすると、localhost で 25 番の通信ポート(デフォルトの SMTP ポートです)を使って実行されている JAMES に接続することができます。

Web アプリケーションのテスト

以下のファイルを含むシンプルな webapp を作成してください。 

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

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: このアプリケーションは Geronimo 2.0 の JavaMail コンポーネントを利用していますので、コンポーネントを見つける場所を指定する必要があります。 これは <dependencies> タグの中にあります。Geronimo が変更されたので、これらのパスを変更して同じ物が参照できるようにする必要があります。<resource-ref> 要素の中に、JavaMail セッションのことがかかれています。これは開発者にサーバー内のリソースの参照を可能にします。<ref-name> 要素は web.xml から利用され、両者は密接に関連しています。<resource-link> エレメントはデフォルトの Geronimo メール・セッションの記述です。

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>

index.jsp:

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>http://localhost:8080/sendmail <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>

ウェブ・アプリケーションのビルドとデプロイ

次のリンクから sendmail アプリケーションをダウンロードしてください。
sendmail

ファイルを解凍すると、sendmail ディレクトリーが作られます。

ソース・コード

SVN からサンプルのソースコードをチェックアウトすることができます。

svn checkout http://svn.apache.org/repos/asf/geronimo/samples/trunk/samples/sendmail

ウェブ・アプリケーションのビルド

sendmail フォルダーにはデプロイできる ear ファイルを含んでいますが、ソースを利用してビルドすることもできます。

コマンドプロンプトを利用して sendmail ディレクトリーへ移動し、mvn install site コマンドを入力するとビルドされます。sendmail フォルダーの下に sendmail-ear-2.0-SNAPSHOT.ear が作られます。

ウェブ・アプリケーションのデプロイ

  1. Console Navigation パネルから Deploy New を選択してください。
  2. Archive 入力欄に sendmail フォルダーの sendmail-ear-2.0-SNAPSHOT.ear を読み込んでください。
  3. Install ボタンを押してアプリケーションをサーバーへデプロイしてください。
  4. send mail ウェブ・アプリケーションをテストする際は、次のリンクを開いてください。http://localhost:8080/sendmail

フォームの内容(From、To、Subject、Message フィールド)を埋めて 'Send' ボタンをクリックしてください。メールが無事送信されると、次のようなメッセージが表示されます。

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