S2Maiの設定備忘録
S2Maiを使うための準備について。
私が仕事で使う時のための備忘録なので、特定の使い方しか記述していません。
設定
S2Maiのサイトから最新版をダウンロード
http://s2mai.sandbox.seasar.org/ja/
S2Mai
S2Mai-Tiger
jarファイルをWEB-INF/libにコピー
- activation
- ozacc-mail
- s2mai
- s2mai-tiger
- freemarker
mailProperties.diconをresourcesにコピー
(s2mai.jarの中から取得)
smtpホストなどを変更
<components namespace="mailProperties"> <component name="host"> "localhost" </component> <component name="port"> 25 </component> <component name="username"> null </component> <component name="password"> null </component> <component name="from"> null </component> <component name="replyTo"> null </component> <component name="returnPath"> null </component> <component name="messageId"> null </component> <component name="templateEncoding"> "UTF-8" </component> <component name="connectionTimeout"> 10000 </component> <component name="readTimeout"> 10000 </component> <component name="mailCharset"> "ISO-2022-JP" </component> </components>
app.diconに下記追加
<include path="s2mai.dicon"/>
creator.diconに下記追加
<component class="org.seasar.mai.creator.MaiCreator" />
customizer.diconに下記追加
<component name="maiAspectCustomizer" class="org.seasar.framework.container.customizer.AspectCustomizer"> <property name="interceptorName">"s2mai.s2MaiInterceptor"</property> </component> <component name="maiCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain"> <initMethod name="addCustomizer"> <arg>maiAspectCustomizer</arg> </initMethod> </component>
ルートパッケージ直下にmaiパッケージを作成
使い方
メール送信に使用する専用のDTOを作成
public class MailDto { public String name; // プログラムからメール本文に渡す値。任意。 public String from; // 送り元 public String[] to; // 送り先(複数指定可) public String[] cc; // CC(複数指定可) public String[] bcc; // BCC(複数指定可) public void setName(String name) // getter,setterが必要 { this.name = name; } public String getName() { return name; } ... }
メールを送信するためのインターフェースを作成(ルートパッケージ.mai直下)
クラス名の最後はMai
public interface MailMai { public void sendTestMail(MailDto dto); }
インターフェースのメソッドごとにFreeMarkerのテンプレートファイルを作成する
置き場所はインターフェースファイルと同じところに。
ファイル名はインターフェース名_メソッド名.ftl。上の例ならMailMai_sendTestMail.ftl。
Subject: テストメール ${data.name}さんこんにちは。
MailLogicなどでメール送信
public MailMai mailMai; ... ... mailMai.sendMail(mailDto); // mailDtoには事前に値を設定しておく。
これだけで送信可能。簡単ですね。
たとえばワークフローとかで承認されたときに飛ぶメールと否認された時に飛ぶメールを分けたい場合は、
インターフェースのメソッドを別にしてftlファイルも別にする形でしょう。
参考にした書籍
- 作者: 竹添直樹
- 出版社/メーカー: 翔泳社
- 発売日: 2010/04/20
- メディア: 大型本
- 購入: 10人 クリック: 270回
- この商品を含むブログ (31件) を見る