1 package com.ozacc.mail.impl;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.Date;
5 import java.util.Properties;
6
7 import javax.mail.AuthenticationFailedException;
8 import javax.mail.MessagingException;
9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.internet.MimeMessage;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.ozacc.mail.Mail;
17 import com.ozacc.mail.MailAuthenticationException;
18 import com.ozacc.mail.MailBuildException;
19 import com.ozacc.mail.MailException;
20 import com.ozacc.mail.MailSendException;
21 import com.ozacc.mail.SendMail;
22
23 /***
24 * SendMailインターフェースの実装クラス。
25 *
26 * @since 1.0
27 * @author Tomohiro Otsuka
28 * @version $Id: SendMailImpl.java,v 1.7 2004/09/20 21:43:15 otsuka Exp $
29 */
30 public class SendMailImpl implements SendMail {
31
32 private static Log log = LogFactory.getLog(SendMailImpl.class);
33
34 /*** デフォ�?トのプ��組コ�?。「smtp」 */
35 public static final String DEFAULT_PROTOCOL = "smtp";
36
37 /***
38 * デフォ�?トのポート。「-1」<br>
39 * -1はプ��組コ�?に�?じた適切なポートを設定す�?特別な値。
40 * */
41 public static final int DEFAULT_PORT = -1;
42
43 /*** デフォ�?トのSMTPサーバ。「localhost」 */
44 public static final String DEFAULT_HOST = "localhost";
45
46 /*** ISO-2022-JP */
47 public static final String JIS_CHARSET = "ISO-2022-JP";
48
49 private static final String RETURN_PATH_KEY = "mail.smtp.from";
50
51 private String protocol = DEFAULT_PROTOCOL;
52
53 private String host = DEFAULT_HOST;
54
55 private int port = DEFAULT_PORT;
56
57 private String username;
58
59 private String password;
60
61 private String charset = JIS_CHARSET;
62
63 private String returnPath;
64
65 private String messageId;
66
67 /***
68 * コンストラクタ。
69 */
70 public SendMailImpl() {}
71
72 /***
73 * コンストラクタ。使用す�?SMTPサーバを指定します。
74 *
75 * @param host SMTPサーバのホスト名、またはIPアド�?ス
76 */
77 public SendMailImpl(String host) {
78 this();
79 setHost(host);
80 }
81
82 /***
83 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail)
84 */
85 public void send(Mail mail) throws MailException {
86 send(new Mail[] { mail });
87 }
88
89 /***
90 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail[])
91 */
92 public void send(Mail[] mails) throws MailException {
93 MimeMessageWrapper[] mmws = new MimeMessageWrapper[mails.length];
94 Session session = Session.getInstance(new Properties());
95 for (int i = 0; i < mails.length; i++) {
96 Mail mail = mails[i];
97
98
99 MimeMessage message = createMimeMessage(session);
100 MimeMessageBuilder builder = new MimeMessageBuilder(message);
101 try {
102 builder.buildMimeMessage(mail);
103 } catch (UnsupportedEncodingException e) {
104 throw new MailBuildException("サポートさ�?ていない文字コードが指定さ�?ました。", e);
105 } catch (MessagingException e) {
106 throw new MailBuildException("MimeMessageの生成に失敗しました。", e);
107 }
108
109
110 String returnPath;
111 if (mail.getReturnPath() != null) {
112 returnPath = mail.getReturnPath().getAddress();
113 } else {
114 returnPath = this.returnPath;
115 }
116
117 mmws[i] = new MimeMessageWrapper(message, returnPath);
118 }
119 processSend(mmws);
120 }
121
122 /***
123 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage)
124 */
125 public void send(MimeMessage message) throws MailException {
126 send(new MimeMessage[] { message });
127 }
128
129 /***
130 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage[])
131 */
132 public void send(MimeMessage[] messages) throws MailException {
133 MimeMessageWrapper[] mmws = new MimeMessageWrapper[messages.length];
134 for (int i = 0; i < messages.length; i++) {
135 mmws[i] = new MimeMessageWrapper(messages[i], returnPath);
136 }
137 processSend(mmws);
138 }
139
140 private void processSend(MimeMessageWrapper[] mmws) throws MailException {
141
142 Session session = Session.getInstance(new Properties());
143
144 Transport transport = null;
145 try {
146
147 log.debug("SMTPサーバ[" + host + "]に接続します。");
148 transport = session.getTransport(protocol);
149 transport.connect(host, port, username, password);
150 log.debug("SMTPサーバ[" + host + "]に接続しました。");
151
152 for (int i = 0; i < mmws.length; i++) {
153 MimeMessage mimeMessage = mmws[i].getMimeMessage();
154 String returnPath = mmws[i].getReturnPath();
155
156
157 if (returnPath != null) {
158 session.getProperties().put(RETURN_PATH_KEY, returnPath);
159 log.debug("Return-Path[" + returnPath + "]を設定しました。");
160 }
161
162
163 mimeMessage.setSentDate(new Date());
164
165 mimeMessage.saveChanges();
166
167
168 log.debug("メー�?を送信します。");
169 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
170 log.debug("メー�?を送信しました。");
171
172
173 if (returnPath != null) {
174 session.getProperties().remove(RETURN_PATH_KEY);
175 log.debug("Return-Path設定をク�?アしました。");
176 }
177 }
178 } catch (AuthenticationFailedException ex) {
179 log.error("SMTPサーバ[" + host + "]への接続認証に失敗しました。", ex);
180 throw new MailAuthenticationException(ex);
181 } catch (MessagingException ex) {
182 log.error("メー�?の送信に失敗しました。", ex);
183 throw new MailSendException("メー�?の送信に失敗しました。", ex);
184 } finally {
185 if (transport != null && transport.isConnected()) {
186 log.debug("SMTPサーバ[" + host + "]との接続を切断します。");
187 try {
188
189 transport.close();
190 } catch (MessagingException e) {
191 log.error("SMTPサーバ[" + host + "]との接続切断に失敗しました。", e);
192 throw new MailException("SMTPサーバ[" + host + "]との接続切断に失敗しました。");
193 }
194 log.debug("SMTPサーバ[" + host + "]との接続を切断しました。");
195 }
196 }
197 }
198
199 /***
200 * 新しいMimeMessageオブジェクトを生成します。<br>
201 * messageIdプ��叢ティがセットさ�?てい�?�?合、OMLMimeMessageのインスタンスを生成します。
202 *
203 * @return 新しいMimeMessageオブジェクト
204 */
205 private MimeMessage createMimeMessage(Session session) {
206 if (messageId == null) {
207 return new MimeMessage(session);
208 }
209 return new OMLMimeMessage(session, messageId);
210 }
211
212 /***
213 * エンコーディングに使用す�?文字コードを返します。
214 *
215 * @return エンコーディングに使用す�?文字コード
216 */
217 public String getCharset() {
218 return charset;
219 }
220
221 /***
222 * メー�?の�?名や本文のエンコーディングに使用す�?文字コードを指定します。
223 * デフォ�?トは<code>ISO-2022-JP</code>です。
224 * <p>
225 * �?本�?環境で利用す�?�?合は通�?変更す�?必要はありません。
226 *
227 * @param charset エンコーディングに使用す�?文字コード
228 */
229 public void setCharset(String charset) {
230 this.charset = charset;
231 }
232
233 /***
234 * セットさ�?たSMTPサーバのホスト名、またはIPアド�?スを返します。
235 *
236 * @return SMTPサーバのホスト名、またはIPアド�?ス
237 */
238 public String getHost() {
239 return host;
240 }
241
242 /***
243 * SMTPサーバのホスト名、またはIPアド�?スをセットします。
244 * デフォ�?トは localhost です。
245 *
246 * @param host SMTPサーバのホスト名、またはIPアド�?ス
247 */
248 public void setHost(String host) {
249 this.host = host;
250 }
251
252 /***
253 * @return SMTPサーバ認証パス�?ード
254 */
255 public String getPassword() {
256 return password;
257 }
258
259 /***
260 * SMTPサーバの接続認証が必要な�?合にパス�?ードをセットします。
261 *
262 * @param password SMTPサーバ認証パス�?ード
263 */
264 public void setPassword(String password) {
265 this.password = password;
266 }
267
268 /***
269 * @return SMTPサーバのポート番�?
270 */
271 public int getPort() {
272 return port;
273 }
274
275 /***
276 * SMTPサーバのポート番号をセットします。
277 *
278 * @param port SMTPサーバのポート番�?
279 */
280 public void setPort(int port) {
281 this.port = port;
282 }
283
284 /***
285 * @return Returns the protocol.
286 */
287 public String getProtocol() {
288 return protocol;
289 }
290
291 /***
292 * @param protocol The protocol to set.
293 */
294 public void setProtocol(String protocol) {
295 this.protocol = protocol;
296 }
297
298 /***
299 * @return Return-Pathアド�?ス
300 */
301 public String getReturnPath() {
302 return returnPath;
303 }
304
305 /***
306 * Return-Pathアド�?スをセットします。
307 * <p>
308 * 送信す�?Mailインスタンスに指定さ�?たFromアド�?ス以外のアド�?スをReturn-Pathとしたい�?合に使用します。
309 * ここでセットさ�?たReturn-Pathより、Mailインスタンスにセットさ�?たReturn-Pathが優先さ�?ます。
310 *
311 * @param returnPath Return-Pathアド�?ス
312 */
313 public void setReturnPath(String returnPath) {
314 this.returnPath = returnPath;
315 }
316
317 /***
318 * @return SMTPサーバ認証ユーザ名
319 */
320 public String getUsername() {
321 return username;
322 }
323
324 /***
325 * SMTPサーバの接続認証が必要な�?合にユーザ名をセットします。
326 *
327 * @param username SMTPサーバ認証ユーザ名
328 */
329 public void setUsername(String username) {
330 this.username = username;
331 }
332
333 /***
334 * 生成さ�?�?MimeMessageに付けら�?�?Message-Idヘッダのドメイン部分を指定します。<br>
335 * 指定さ�?ない�?�?(nullや空文字列の�?�?)は、JavaMailがMessage-Idヘッダを生成します。
336 * JavaMailが生成す�?「JavaMail.実行ユーザ名@ホスト名」のMessage-Idを避けたい�?合に、このメソッドを使用します。
337 * <p>
338 * messageIdプ��叢ティがセットさ�?てい�?�?合、Mailから生成さ�?�?MimeMessageのMessage-Idには
339 * <code>タイムスタンプ + ランダムに生成さ�?�?16桁の数値 + ここでセットさ�?た値</code>
340 * が使用さ�?ます。
341 * <p>
342 * 生成さ�?�?Message-Idの例。 (実際の数値部分は送信メー�?毎に変�?ります)<ul>
343 * <li>messageIdに'example.com'を指定した�?合・・・1095714924963.5619528074501343@example.com</li>
344 * <li>messageIdに'@example.com'を指定した�?合・・・1095714924963.5619528074501343@example.com (上と同じ)</li>
345 * <li>messageIdに'OML@example.com'を指定した�?合・・・1095714924963.5619528074501343.OML@example.com</li>
346 * <li>messageIdに'.OML@example.com'を指定した�?合・・・1095714924963.5619528074501343.OML@example.com (上と同じ)</li>
347 * </ul>
348 * <p>
349 * <strong>�?:</strong> このMessage-Idは<code>send(Mail)</code>か<code>send(Mail[])</code>メソッドが呼びだ�?た�?にのみ有効です。MimeMessageを直接送信す�?�?合には適用さ�?ません。
350 *
351 * @param messageId メー�?に付けら�?�?Message-Idヘッダのドメイン部分
352 * @throws IllegalArgumentException @を複数含んだ文字列を指定した�?�?
353 */
354 public void setMessageId(String messageId) {
355 if (messageId == null || messageId.length() < 1) {
356 return;
357 }
358
359 String[] parts = messageId.split("@");
360 if (parts.length > 2) {
361 throw new IllegalArgumentException("messageIdプ��叢ティに'@'を複数含むことはできません。[" + messageId
362 + "]");
363 }
364
365 this.messageId = messageId;
366 }
367
368 /***
369 * MimeMessageインスタンスと、そのメー�?に対�?す�?Return-Pathをラップす�?クラス。
370 *
371 * @author Tomohiro Otsuka
372 * @version $Id: SendMailImpl.java,v 1.7 2004/09/20 21:43:15 otsuka Exp $
373 */
374 private static class MimeMessageWrapper {
375
376 private MimeMessage mimeMessage;
377
378 private String returnPath;
379
380 public MimeMessageWrapper(MimeMessage mimeMessage, String returnPath) {
381 this.mimeMessage = mimeMessage;
382 this.returnPath = returnPath;
383 }
384
385 public MimeMessage getMimeMessage() {
386 return mimeMessage;
387 }
388
389 public String getReturnPath() {
390 return returnPath;
391 }
392
393 }
394
395 }