View Javadoc

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