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