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