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.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.NotConnectedException;
22  import com.ozacc.mail.SendMailPro;
23  
24  /***
25   * SendMailProインターフェースの実装クラス。
26   * 
27   * @author Tomohiro Otsuka
28   * @version $Id: SendMailProImpl.java,v 1.2 2004/09/05 07:52:45 otsuka Exp $
29   */
30  public class SendMailProImpl implements SendMailPro {
31  
32  	/*** smtp */
33  	public static final String DEFAULT_PROTOCOL = "smtp";
34  
35  	/*** -1 */
36  	public static final int DEFAULT_PORT = -1;
37  
38  	/*** localhost */
39  	public static final String DEFAULT_HOST = "localhost";
40  
41  	/*** ISO-2022-JP */
42  	public static final String JIS_CHARSET = "ISO-2022-JP";
43  
44  	private static final String RETURN_PATH_KEY = "mail.smtp.from";
45  
46  	private static Log log = LogFactory.getLog(SendMailProImpl.class);
47  
48  	private String protocol = DEFAULT_PROTOCOL;
49  
50  	private String host = DEFAULT_HOST;
51  
52  	private int port = DEFAULT_PORT;
53  
54  	private String username;
55  
56  	private String password;
57  
58  	private String charset = JIS_CHARSET;
59  
60  	private String returnPath;
61  
62  	private Session session = Session.getInstance(new Properties());
63  
64  	private Transport transport;
65  
66  	private boolean connected;
67  
68  	/***
69  	 * コンストラクタ。
70  	 */
71  	public SendMailProImpl() {}
72  
73  	/***
74  	 * コンストラクタ。使用す�?SMTPサーバを指定します。
75  	 * 
76  	 * @param host SMTPサーバのホスト名、またはIPアド�?ス
77  	 */
78  	public SendMailProImpl(String host) {
79  		this();
80  		setHost(host);
81  	}
82  
83  	/***
84  	 * @see com.ozacc.mail.SendMailPro#connect()
85  	 */
86  	public synchronized void connect() throws MailException {
87  		// グ��充バ�?Return-Pathの設�?
88  		putOnReturnPath(this.returnPath);
89  
90  		try {
91  			// SMTPサーバに接続
92  			log.debug("SMTPサーバ[" + host + "]に接続します。");
93  
94  			transport = session.getTransport(protocol);
95  			transport.connect(host, port, username, password);
96  		} catch (AuthenticationFailedException ex) {
97  			log.error("SMTPサーバ[" + host + "]への接続認証に失敗しました。", ex);
98  			throw new MailAuthenticationException(ex);
99  		} catch (MessagingException ex) {
100 			log.error("SMTPサーバ[" + host + "]への接続に失敗しました。", ex);
101 			throw new MailSendException("SMTPサーバ[" + host + "]への接続に失敗しました。", ex);
102 		}
103 
104 		log.debug("SMTPサーバ[" + host + "]に接続しました。");
105 
106 		connected = true;
107 	}
108 
109 	/***
110 	 * @see com.ozacc.mail.SendMailPro#disconnect()
111 	 */
112 	public synchronized void disconnect() throws MailException {
113 		if (connected) {
114 			try {
115 				log.debug("SMTPサーバ[" + host + "]との接続を切断します。");
116 
117 				// SMTPサーバとの接続を切断
118 				transport.close();
119 				connected = false;
120 
121 				log.debug("SMTPサーバ[" + host + "]との接続を切断しました。");
122 			} catch (MessagingException ex) {
123 				log.error("SMTPサーバ[" + host + "]との接続切断に失敗しました。", ex);
124 				throw new MailException("SMTPサーバ[" + host + "]との接続切断に失敗しました。");
125 			} finally {
126 				// グ��充バ�?Return-Pathの解�?
127 				releaseReturnPath(false);
128 			}
129 		} else {
130 			log.warn("SMTPサーバ[" + host + "]との接続が確立さ�?ていない状態で、接続の切断が�?クエストさ�?ました。");
131 		}
132 	}
133 
134 	/***
135 	 * ReturnPathをセットします。
136 	 * 
137 	 * @param returnPath
138 	 */
139 	private void putOnReturnPath(String returnPath) {
140 		if (returnPath != null) {
141 			session.getProperties().put(RETURN_PATH_KEY, returnPath);
142 			log.debug("Return-Path[" + returnPath + "]を設定しました。");
143 		}
144 	}
145 
146 	/***
147 	 * ReturnPathの設定をク�?アします。
148 	 * <p>
149 	 * setGlobalReturnPathAgainがtrueに指定さ�?てい�?�?合、�?旦Return-Path設定をク�?アした後に、
150 	 * グ��充バ�?なReturn-Path(setReturnPath()メソッドで、このインスタンスにセットさ�?たReturn-Pathアド�?ス)を設定します。
151 	 * グ��充バ�?なReturn-Pathがセットさ�?ていなけ�?ばReturn-Pathはク�?アさ�?たままになります。
152 	 * <p>
153 	 * ク�?アさ�?た状態でsend()メソッドが実行さ�?�?と、Fromの値がReturn-Pathに使用さ�?ます。
154 	 * 
155 	 * @param setGlobalReturnPathAgain Return-Path設定をク�?アした後、再度グ��充バ�?なReturn-Pathをセットす�?�?�? true
156 	 */
157 	private void releaseReturnPath(boolean setGlobalReturnPathAgain) {
158 		session.getProperties().remove(RETURN_PATH_KEY);
159 		log.debug("Return-Path設定をク�?アしました。");
160 
161 		if (setGlobalReturnPathAgain && this.returnPath != null) {
162 			putOnReturnPath(this.returnPath);
163 		}
164 	}
165 
166 	/***
167 	 * @see com.ozacc.mail.SendMailPro#send(javax.mail.internet.MimeMessage)
168 	 */
169 	public void send(MimeMessage mimeMessage) throws MailException {
170 		if (!connected) {
171 			log.error("SMTPサーバへの接続が確立さ�?ていません。");
172 			throw new NotConnectedException("SMTPサーバへの接続が確立さ�?ていません。");
173 		}
174 
175 		try {
176 			// 送信�?�?をセット
177 			mimeMessage.setSentDate(new Date());
178 			mimeMessage.saveChanges();
179 			// 送信
180 			log.debug("メー�?を送信します。");
181 			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
182 			log.debug("メー�?を送信しました。");
183 		} catch (MessagingException ex) {
184 			log.error("メー�?の送信に失敗しました。", ex);
185 			throw new MailSendException("メー�?の送信に失敗しました。", ex);
186 		}
187 	}
188 
189 	/***
190 	 * @see com.ozacc.mail.SendMailPro#send(com.ozacc.mail.Mail)
191 	 */
192 	public void send(Mail mail) throws MailException {
193 		if (mail.getReturnPath() != null) {
194 			sendMailWithReturnPath(mail);
195 		} else {
196 			sendMail(mail);
197 		}
198 	}
199 
200 	/***
201 	 * 指定さ�?たMailからMimeMessageを生成し、send(MimeMessage)メソッドに渡します。
202 	 * 
203 	 * @param mail
204 	 * @throws MailException
205 	 */
206 	private void sendMail(Mail mail) throws MailException {
207 		// MimeMessageの生成
208 		MimeMessage message = createMimeMessage();
209 		MimeMessageBuilder builder = new MimeMessageBuilder(message, charset);
210 		try {
211 			builder.buildMimeMessage(mail);
212 		} catch (UnsupportedEncodingException e) {
213 			throw new MailBuildException("サポートさ�?ていない文字コードが指定さ�?ました。", e);
214 		} catch (MessagingException e) {
215 			throw new MailBuildException("MimeMessageの生成に失敗しました。", e);
216 		}
217 		// 送信
218 		send(message);
219 	}
220 
221 	/***
222 	 * 指定さ�?たMailにセットさ�?たReturn-Pathを設定して、メー�?を送信します。
223 	 * 同�?メソッドです。
224 	 * 
225 	 * @param mail
226 	 * @throws MailException
227 	 */
228 	private synchronized void sendMailWithReturnPath(Mail mail) throws MailException {
229 		putOnReturnPath(mail.getReturnPath().getAddress());
230 
231 		sendMail(mail);
232 
233 		releaseReturnPath(true);
234 	}
235 
236 	/***
237 	 * 新しいMimeMessageオブジェクトを生成します。
238 	 * 
239 	 * @return 新しいMimeMessageオブジェクト
240 	 */
241 	public MimeMessage createMimeMessage() {
242 		return new MimeMessage(session);
243 	}
244 
245 	/***
246 	 * @return Returns the session.
247 	 */
248 	protected Session getSession() {
249 		return session;
250 	}
251 
252 	/***
253 	 * エンコーディングに使用す�?文字コードを返します。
254 	 * 
255 	 * @return エンコーディングに使用す�?文字コード
256 	 */
257 	public String getCharset() {
258 		return charset;
259 	}
260 
261 	/***
262 	 * メー�?の�?名や本文のエンコーディングに使用す�?文字コードを指定します。
263 	 * デフォ�?トは ISO-2022-JP です。
264 	 * <p>
265 	 * �?本�?環境で利用す�?�?合は通�?変更す�?必要はありません。
266 	 * 
267 	 * @param charset エンコーディングに使用す�?文字コード
268 	 */
269 	public void setCharset(String charset) {
270 		this.charset = charset;
271 	}
272 
273 	/***
274 	 * @return Returns the host.
275 	 */
276 	public String getHost() {
277 		return host;
278 	}
279 
280 	/***
281 	 * SMTPサーバのホスト名、またはIPアド�?スをセットします。
282 	 * デフォ�?トは localhost です。
283 	 * 
284 	 * @param host SMTPサーバのホスト名、またはIPアド�?ス
285 	 */
286 	public void setHost(String host) {
287 		this.host = host;
288 	}
289 
290 	/***
291 	 * @return SMTPサーバ認証パス�?ード
292 	 */
293 	public String getPassword() {
294 		return password;
295 	}
296 
297 	/***
298 	 * SMTPサーバの接続認証が必要な�?合にパス�?ードをセットします。
299 	 * 
300 	 * @param password SMTPサーバ認証パス�?ード
301 	 */
302 	public void setPassword(String password) {
303 		this.password = password;
304 	}
305 
306 	/***
307 	 * @return SMTPサーバのポート番�?
308 	 */
309 	public int getPort() {
310 		return port;
311 	}
312 
313 	/***
314 	 * SMTPサーバのポート番号をセットします。
315 	 * 
316 	 * @param port SMTPサーバのポート番�?
317 	 */
318 	public void setPort(int port) {
319 		this.port = port;
320 	}
321 
322 	/***
323 	 * @return Returns the protocol.
324 	 */
325 	public String getProtocol() {
326 		return protocol;
327 	}
328 
329 	/***
330 	 * @param protocol The protocol to set.
331 	 */
332 	public void setProtocol(String protocol) {
333 		this.protocol = protocol;
334 	}
335 
336 	/***
337 	 * @return Return-Pathアド�?ス
338 	 */
339 	public String getReturnPath() {
340 		return returnPath;
341 	}
342 
343 	/***
344 	 * Return-Pathアド�?スをセットします。
345 	 * <p>
346 	 * 送信す�?Mailインスタンスに指定さ�?たFromアド�?ス以外のアド�?スをReturn-Pathとしたい�?合に使用します。
347 	 * ここでセットさ�?たReturn-Pathより、Mailインスタンスにセットさ�?たReturn-Pathが優先さ�?ます。
348 	 * 
349 	 * @param returnPath Return-Pathアド�?ス
350 	 */
351 	public void setReturnPath(String returnPath) {
352 		this.returnPath = returnPath;
353 	}
354 
355 	/***
356 	 * @return SMTPサーバ認証ユーザ名
357 	 */
358 	public String getUsername() {
359 		return username;
360 	}
361 
362 	/***
363 	 * SMTPサーバの接続認証が必要な�?合にユーザ名をセットします。
364 	 * 
365 	 * @param username SMTPサーバ認証ユーザ名
366 	 */
367 	public void setUsername(String username) {
368 		this.username = username;
369 	}
370 
371 }