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