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 * @author Tomohiro Otsuka
27 * @version $Id: SendMailImpl.java,v 1.5 2004/09/05 16:16:23 otsuka Exp $
28 */
29 public class SendMailImpl implements SendMail {
30
31 private static Log log = LogFactory.getLog(SendMailImpl.class);
32
33 /*** ¥Ç¥Õ¥©¥?¥È¤Î¥×¥úÁÈ¥³¥?¡£¡Ösmtp¡× */
34 public static final String DEFAULT_PROTOCOL = "smtp";
35
36 /***
37 * ¥Ç¥Õ¥©¥?¥È¤Î¥Ý¡¼¥È¡£¡Ö-1¡×<br>
38 * -1¤Ï¥×¥úÁÈ¥³¥?¤Ë±?¤¸¤¿Å¬Àڤʥݡ¼¥È¤òÀßÄꤹ¤?ÆÃÊ̤ÊÃÍ¡£
39 * */
40 public static final int DEFAULT_PORT = -1;
41
42 /*** ¥Ç¥Õ¥©¥?¥È¤ÎSMTP¥µ¡¼¥Ð¡£¡Ölocalhost¡× */
43 public static final String DEFAULT_HOST = "localhost";
44
45 /*** ISO-2022-JP */
46 public static final String JIS_CHARSET = "ISO-2022-JP";
47
48 private static final String RETURN_PATH_KEY = "mail.smtp.from";
49
50 private String protocol = DEFAULT_PROTOCOL;
51
52 private String host = DEFAULT_HOST;
53
54 private int port = DEFAULT_PORT;
55
56 private String username;
57
58 private String password;
59
60 private String charset = JIS_CHARSET;
61
62 private String returnPath;
63
64 /***
65 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
66 */
67 public SendMailImpl() {}
68
69 /***
70 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£»ÈÍѤ¹¤?SMTP¥µ¡¼¥Ð¤ò»ØÄꤷ¤Þ¤¹¡£
71 *
72 * @param host SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
73 */
74 public SendMailImpl(String host) {
75 this();
76 setHost(host);
77 }
78
79 /***
80 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail)
81 */
82 public void send(Mail mail) throws MailException {
83 send(new Mail[] { mail });
84 }
85
86 /***
87 * @see com.ozacc.mail.SendMail#send(com.ozacc.mail.Mail[])
88 */
89 public void send(Mail[] mails) throws MailException {
90 MimeMessageWrapper[] mmws = new MimeMessageWrapper[mails.length];
91 Session session = Session.getInstance(new Properties());
92 for (int i = 0; i < mails.length; i++) {
93 Mail mail = mails[i];
94
95
96 MimeMessage message = createMimeMessage(session);
97 MimeMessageBuilder builder = new MimeMessageBuilder(message);
98 try {
99 builder.buildMimeMessage(mail);
100 } catch (UnsupportedEncodingException e) {
101 throw new MailBuildException("¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤Ê¸»ú¥³¡¼¥É¤¬»ØÄꤵ¤?¤Þ¤·¤¿¡£", e);
102 } catch (MessagingException e) {
103 throw new MailBuildException("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
104 }
105
106
107 String returnPath;
108 if (mail.getReturnPath() != null) {
109 returnPath = mail.getReturnPath().getAddress();
110 } else {
111 returnPath = this.returnPath;
112 }
113
114 mmws[i] = new MimeMessageWrapper(message, returnPath);
115 }
116 processSend(mmws);
117 }
118
119 /***
120 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage)
121 */
122 public void send(MimeMessage message) throws MailException {
123 send(new MimeMessage[] { message });
124 }
125
126 /***
127 * @see com.ozacc.mail.SendMail#send(javax.mail.internet.MimeMessage[])
128 */
129 public void send(MimeMessage[] messages) throws MailException {
130 MimeMessageWrapper[] mmws = new MimeMessageWrapper[messages.length];
131 for (int i = 0; i < messages.length; i++) {
132 mmws[i] = new MimeMessageWrapper(messages[i], returnPath);
133 }
134 processSend(mmws);
135 }
136
137 private void processSend(MimeMessageWrapper[] mmws) throws MailException {
138
139 Session session = Session.getInstance(new Properties());
140
141 Transport transport = null;
142 try {
143
144 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤¹¡£");
145 transport = session.getTransport(protocol);
146 transport.connect(host, port, username, password);
147 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤·¤¿¡£");
148
149 for (int i = 0; i < mmws.length; i++) {
150 MimeMessage mimeMessage = mmws[i].getMimeMessage();
151 String returnPath = mmws[i].getReturnPath();
152
153
154 if (returnPath != null) {
155 session.getProperties().put(RETURN_PATH_KEY, returnPath);
156 log.debug("Return-Path[" + returnPath + "]¤òÀßÄꤷ¤Þ¤·¤¿¡£");
157 }
158
159
160 mimeMessage.setSentDate(new Date());
161 mimeMessage.saveChanges();
162
163 log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤¹¡£");
164 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
165 log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤·¤¿¡£");
166
167
168 if (returnPath != null) {
169 session.getProperties().remove(RETURN_PATH_KEY);
170 log.debug("Return-PathÀßÄê¤ò¥¯¥?¥¢¤·¤Þ¤·¤¿¡£");
171 }
172 }
173 } catch (AuthenticationFailedException ex) {
174 log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳǧ¾Ú¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
175 throw new MailAuthenticationException(ex);
176 } catch (MessagingException ex) {
177 log.error("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
178 throw new MailSendException("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
179 } finally {
180 if (transport != null && transport.isConnected()) {
181 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£");
182 try {
183
184 transport.close();
185 } catch (MessagingException e) {
186 log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
187 throw new MailException("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£");
188 }
189 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤·¤¿¡£");
190 }
191 }
192 }
193
194 /***
195 * ¿·¤·¤¤MimeMessage¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤·¤Þ¤¹¡£
196 *
197 * @return ¿·¤·¤¤MimeMessage¥ª¥Ö¥¸¥§¥¯¥È
198 */
199 private MimeMessage createMimeMessage(Session session) {
200 return new MimeMessage(session);
201 }
202
203 /***
204 * ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
205 *
206 * @return ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
207 */
208 public String getCharset() {
209 return charset;
210 }
211
212 /***
213 * ¥á¡¼¥?¤Î·?̾¤äËÜʸ¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
214 * ¥Ç¥Õ¥©¥?¥È¤Ï<code>ISO-2022-JP</code>¤Ç¤¹¡£
215 * <p>
216 * Æ?Ëܸ?´Ä¶¤ÇÍøÍѤ¹¤?¾?¹ç¤ÏÄ̾?Êѹ¹¤¹¤?ɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
217 *
218 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
219 */
220 public void setCharset(String charset) {
221 this.charset = charset;
222 }
223
224 /***
225 * ¥»¥Ã¥È¤µ¤?¤¿SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹¤òÊÖ¤·¤Þ¤¹¡£
226 *
227 * @return SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
228 */
229 public String getHost() {
230 return host;
231 }
232
233 /***
234 * SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
235 * ¥Ç¥Õ¥©¥?¥È¤Ï localhost ¤Ç¤¹¡£
236 *
237 * @param host SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
238 */
239 public void setHost(String host) {
240 this.host = host;
241 }
242
243 /***
244 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
245 */
246 public String getPassword() {
247 return password;
248 }
249
250 /***
251 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥Ñ¥¹¥?¡¼¥É¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
252 *
253 * @param password SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
254 */
255 public void setPassword(String password) {
256 this.password = password;
257 }
258
259 /***
260 * @return SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
261 */
262 public int getPort() {
263 return port;
264 }
265
266 /***
267 * SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹æ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
268 *
269 * @param port SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
270 */
271 public void setPort(int port) {
272 this.port = port;
273 }
274
275 /***
276 * @return Returns the protocol.
277 */
278 public String getProtocol() {
279 return protocol;
280 }
281
282 /***
283 * @param protocol The protocol to set.
284 */
285 public void setProtocol(String protocol) {
286 this.protocol = protocol;
287 }
288
289 /***
290 * @return Return-Path¥¢¥É¥?¥¹
291 */
292 public String getReturnPath() {
293 return returnPath;
294 }
295
296 /***
297 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
298 * <p>
299 * Á÷¿®¤¹¤?Mail¥¤¥ó¥¹¥¿¥ó¥¹¤Ë»ØÄꤵ¤?¤¿From¥¢¥É¥?¥¹°Ê³°¤Î¥¢¥É¥?¥¹¤òReturn-Path¤È¤·¤¿¤¤¾?¹ç¤Ë»ÈÍѤ·¤Þ¤¹¡£
300 * ¤³¤³¤Ç¥»¥Ã¥È¤µ¤?¤¿Return-Path¤è¤ê¡¢Mail¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¥»¥Ã¥È¤µ¤?¤¿Return-Path¤¬Í¥À褵¤?¤Þ¤¹¡£
301 *
302 * @param returnPath Return-Path¥¢¥É¥?¥¹
303 */
304 public void setReturnPath(String returnPath) {
305 this.returnPath = returnPath;
306 }
307
308 /***
309 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
310 */
311 public String getUsername() {
312 return username;
313 }
314
315 /***
316 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥æ¡¼¥¶Ì¾¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
317 *
318 * @param username SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
319 */
320 public void setUsername(String username) {
321 this.username = username;
322 }
323
324 /***
325 * MimeMessage¥¤¥ó¥¹¥¿¥ó¥¹¤È¡¢¤½¤Î¥á¡¼¥?¤ËÂб?¤¹¤?Return-Path¤ò¥é¥Ã¥×¤¹¤?¥¯¥é¥¹¡£
326 *
327 * @author Tomohiro Otsuka
328 * @version $Id: SendMailImpl.java,v 1.5 2004/09/05 16:16:23 otsuka Exp $
329 */
330 private static class MimeMessageWrapper {
331
332 private MimeMessage mimeMessage;
333
334 private String returnPath;
335
336 public MimeMessageWrapper(MimeMessage mimeMessage, String returnPath) {
337 this.mimeMessage = mimeMessage;
338 this.returnPath = returnPath;
339 }
340
341 public MimeMessage getMimeMessage() {
342 return mimeMessage;
343 }
344
345 public String getReturnPath() {
346 return returnPath;
347 }
348
349 }
350
351 }