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