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.4 2004/09/21 20:54:40 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 private String messageId;
70
71 /***
72 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
73 */
74 public SendMailProImpl() {}
75
76 /***
77 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£»ÈÍѤ¹¤?SMTP¥µ¡¼¥Ð¤ò»ØÄꤷ¤Þ¤¹¡£
78 *
79 * @param host SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
80 */
81 public SendMailProImpl(String host) {
82 this();
83 setHost(host);
84 }
85
86 /***
87 * @see com.ozacc.mail.SendMailPro#connect()
88 */
89 public synchronized void connect() throws MailException {
90
91 putOnReturnPath(this.returnPath);
92
93 try {
94
95 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤¹¡£");
96
97 transport = session.getTransport(protocol);
98 transport.connect(host, port, username, password);
99 } catch (AuthenticationFailedException ex) {
100 log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳǧ¾Ú¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
101 throw new MailAuthenticationException(ex);
102 } catch (MessagingException ex) {
103 log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
104 throw new MailSendException("SMTP¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
105 }
106
107 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤·¤¿¡£");
108
109 connected = true;
110 }
111
112 /***
113 * @see com.ozacc.mail.SendMailPro#disconnect()
114 */
115 public synchronized void disconnect() throws MailException {
116 if (connected) {
117 try {
118 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£");
119
120
121 transport.close();
122 connected = false;
123
124 log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤·¤¿¡£");
125 } catch (MessagingException ex) {
126 log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
127 throw new MailException("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£");
128 } finally {
129
130 releaseReturnPath(false);
131 }
132 } else {
133 log.warn("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤¬³ÎΩ¤µ¤?¤Æ¤¤¤Ê¤¤¾õÂ֤ǡ¢Àܳ¤ÎÀÚÃǤ¬¥?¥¯¥¨¥¹¥È¤µ¤?¤Þ¤·¤¿¡£");
134 }
135 }
136
137 /***
138 * ReturnPath¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
139 *
140 * @param returnPath
141 */
142 private void putOnReturnPath(String returnPath) {
143 if (returnPath != null) {
144 session.getProperties().put(RETURN_PATH_KEY, returnPath);
145 log.debug("Return-Path[" + returnPath + "]¤òÀßÄꤷ¤Þ¤·¤¿¡£");
146 }
147 }
148
149 /***
150 * ReturnPath¤ÎÀßÄê¤ò¥¯¥?¥¢¤·¤Þ¤¹¡£
151 * <p>
152 * setGlobalReturnPathAgain¤¬true¤Ë»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¡¢°?öReturn-PathÀßÄê¤ò¥¯¥?¥¢¤·¤¿¸å¤Ë¡¢
153 * ¥°¥ú½¼¥Ð¥?¤ÊReturn-Path(setReturnPath()¥á¥½¥Ã¥É¤Ç¡¢¤³¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¥»¥Ã¥È¤µ¤?¤¿Return-Path¥¢¥É¥?¥¹)¤òÀßÄꤷ¤Þ¤¹¡£
154 * ¥°¥ú½¼¥Ð¥?¤ÊReturn-Path¤¬¥»¥Ã¥È¤µ¤?¤Æ¤¤¤Ê¤±¤?¤ÐReturn-Path¤Ï¥¯¥?¥¢¤µ¤?¤¿¤Þ¤Þ¤Ë¤Ê¤ê¤Þ¤¹¡£
155 * <p>
156 * ¥¯¥?¥¢¤µ¤?¤¿¾õÂÖ¤Çsend()¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤?¤?¤È¡¢From¤ÎÃͤ¬Return-Path¤Ë»ÈÍѤµ¤?¤Þ¤¹¡£
157 *
158 * @param setGlobalReturnPathAgain Return-PathÀßÄê¤ò¥¯¥?¥¢¤·¤¿¸å¡¢ºÆÅÙ¥°¥ú½¼¥Ð¥?¤ÊReturn-Path¤ò¥»¥Ã¥È¤¹¤?¾?¹? true
159 */
160 private void releaseReturnPath(boolean setGlobalReturnPathAgain) {
161 session.getProperties().remove(RETURN_PATH_KEY);
162 log.debug("Return-PathÀßÄê¤ò¥¯¥?¥¢¤·¤Þ¤·¤¿¡£");
163
164 if (setGlobalReturnPathAgain && this.returnPath != null) {
165 putOnReturnPath(this.returnPath);
166 }
167 }
168
169 /***
170 * @see com.ozacc.mail.SendMailPro#send(javax.mail.internet.MimeMessage)
171 */
172 public void send(MimeMessage mimeMessage) throws MailException {
173 if (!connected) {
174 log.error("SMTP¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤¬³ÎΩ¤µ¤?¤Æ¤¤¤Þ¤»¤ó¡£");
175 throw new NotConnectedException("SMTP¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤¬³ÎΩ¤µ¤?¤Æ¤¤¤Þ¤»¤ó¡£");
176 }
177
178 try {
179
180 mimeMessage.setSentDate(new Date());
181 mimeMessage.saveChanges();
182
183 log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤¹¡£");
184 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
185 log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤·¤¿¡£");
186 } catch (MessagingException ex) {
187 log.error("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
188 throw new MailSendException("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex);
189 }
190 }
191
192 /***
193 * @see com.ozacc.mail.SendMailPro#send(com.ozacc.mail.Mail)
194 */
195 public void send(Mail mail) throws MailException {
196 if (mail.getReturnPath() != null) {
197 sendMailWithReturnPath(mail);
198 } else {
199 sendMail(mail);
200 }
201 }
202
203 /***
204 * »ØÄꤵ¤?¤¿Mail¤«¤éMimeMessage¤òÀ¸À®¤·¡¢send(MimeMessage)¥á¥½¥Ã¥É¤ËÅϤ·¤Þ¤¹¡£
205 *
206 * @param mail
207 * @throws MailException
208 */
209 private void sendMail(Mail mail) throws MailException {
210
211 MimeMessage message = createMimeMessage();
212 MimeMessageBuilder builder = new MimeMessageBuilder(message, charset);
213 try {
214 builder.buildMimeMessage(mail);
215 } catch (UnsupportedEncodingException e) {
216 throw new MailBuildException("¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤Ê¸»ú¥³¡¼¥É¤¬»ØÄꤵ¤?¤Þ¤·¤¿¡£", e);
217 } catch (MessagingException e) {
218 throw new MailBuildException("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
219 }
220
221 send(message);
222 }
223
224 /***
225 * »ØÄꤵ¤?¤¿Mail¤Ë¥»¥Ã¥È¤µ¤?¤¿Return-Path¤òÀßÄꤷ¤Æ¡¢¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤¹¡£
226 * Ʊ´?¥á¥½¥Ã¥É¤Ç¤¹¡£
227 *
228 * @param mail
229 * @throws MailException
230 */
231 private synchronized void sendMailWithReturnPath(Mail mail) throws MailException {
232 putOnReturnPath(mail.getReturnPath().getAddress());
233
234 sendMail(mail);
235
236 releaseReturnPath(true);
237 }
238
239 /***
240 * ¿·¤·¤¤MimeMessage¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤·¤Þ¤¹¡£
241 *
242 * @return ¿·¤·¤¤MimeMessage¥ª¥Ö¥¸¥§¥¯¥È
243 */
244 public MimeMessage createMimeMessage() {
245 if (messageId == null) {
246 return new MimeMessage(session);
247 }
248 return new OMLMimeMessage(session, messageId);
249 }
250
251 /***
252 * @return Returns the session.
253 */
254 protected Session getSession() {
255 return session;
256 }
257
258 /***
259 * ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
260 *
261 * @return ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
262 */
263 public String getCharset() {
264 return charset;
265 }
266
267 /***
268 * ¥á¡¼¥?¤Î·?̾¤äËÜʸ¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
269 * ¥Ç¥Õ¥©¥?¥È¤Ï ISO-2022-JP ¤Ç¤¹¡£
270 * <p>
271 * Æ?Ëܸ?´Ä¶¤ÇÍøÍѤ¹¤?¾?¹ç¤ÏÄ̾?Êѹ¹¤¹¤?ɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
272 *
273 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
274 */
275 public void setCharset(String charset) {
276 this.charset = charset;
277 }
278
279 /***
280 * @return Returns the host.
281 */
282 public String getHost() {
283 return host;
284 }
285
286 /***
287 * SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
288 * ¥Ç¥Õ¥©¥?¥È¤Ï localhost ¤Ç¤¹¡£
289 *
290 * @param host SMTP¥µ¡¼¥Ð¤Î¥Û¥¹¥È̾¡¢¤Þ¤¿¤ÏIP¥¢¥É¥?¥¹
291 */
292 public void setHost(String host) {
293 this.host = host;
294 }
295
296 /***
297 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
298 */
299 public String getPassword() {
300 return password;
301 }
302
303 /***
304 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥Ñ¥¹¥?¡¼¥É¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
305 *
306 * @param password SMTP¥µ¡¼¥Ðǧ¾Ú¥Ñ¥¹¥?¡¼¥É
307 */
308 public void setPassword(String password) {
309 this.password = password;
310 }
311
312 /***
313 * @return SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
314 */
315 public int getPort() {
316 return port;
317 }
318
319 /***
320 * SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹æ¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
321 *
322 * @param port SMTP¥µ¡¼¥Ð¤Î¥Ý¡¼¥ÈÈÖ¹?
323 */
324 public void setPort(int port) {
325 this.port = port;
326 }
327
328 /***
329 * @return Returns the protocol.
330 */
331 public String getProtocol() {
332 return protocol;
333 }
334
335 /***
336 * @param protocol The protocol to set.
337 */
338 public void setProtocol(String protocol) {
339 this.protocol = protocol;
340 }
341
342 /***
343 * @return Return-Path¥¢¥É¥?¥¹
344 */
345 public String getReturnPath() {
346 return returnPath;
347 }
348
349 /***
350 * Return-Path¥¢¥É¥?¥¹¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
351 * <p>
352 * Á÷¿®¤¹¤?Mail¥¤¥ó¥¹¥¿¥ó¥¹¤Ë»ØÄꤵ¤?¤¿From¥¢¥É¥?¥¹°Ê³°¤Î¥¢¥É¥?¥¹¤òReturn-Path¤È¤·¤¿¤¤¾?¹ç¤Ë»ÈÍѤ·¤Þ¤¹¡£
353 * ¤³¤³¤Ç¥»¥Ã¥È¤µ¤?¤¿Return-Path¤è¤ê¡¢Mail¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¥»¥Ã¥È¤µ¤?¤¿Return-Path¤¬Í¥À褵¤?¤Þ¤¹¡£
354 *
355 * @param returnPath Return-Path¥¢¥É¥?¥¹
356 */
357 public void setReturnPath(String returnPath) {
358 this.returnPath = returnPath;
359 }
360
361 /***
362 * @return SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
363 */
364 public String getUsername() {
365 return username;
366 }
367
368 /***
369 * SMTP¥µ¡¼¥Ð¤ÎÀܳǧ¾Ú¤¬É¬Íפʾ?¹ç¤Ë¥æ¡¼¥¶Ì¾¤ò¥»¥Ã¥È¤·¤Þ¤¹¡£
370 *
371 * @param username SMTP¥µ¡¼¥Ðǧ¾Ú¥æ¡¼¥¶Ì¾
372 */
373 public void setUsername(String username) {
374 this.username = username;
375 }
376
377 /***
378 * À¸À®¤µ¤?¤?MimeMessage¤ËÉÕ¤±¤é¤?¤?Message-Id¥Ø¥Ã¥À¤Î¥É¥á¥¤¥óÉôʬ¤ò»ØÄꤷ¤Þ¤¹¡£<br>
379 * »ØÄꤵ¤?¤Ê¤¤¾?¹?(null¤ä¶õʸ»úÎó¤Î¾?¹?)¤Ï¡¢JavaMail¤¬Message-Id¥Ø¥Ã¥À¤òÀ¸À®¤·¤Þ¤¹¡£
380 * JavaMail¤¬À¸À®¤¹¤?¡ÖJavaMail.¼Â¹Ô¥æ¡¼¥¶Ì¾@¥Û¥¹¥È̾¡×¤ÎMessage-Id¤òÈò¤±¤¿¤¤¾?¹ç¤Ë¡¢¤³¤Î¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤Þ¤¹¡£
381 * <p>
382 * messageId¥×¥úÁѥƥ£¤¬¥»¥Ã¥È¤µ¤?¤Æ¤¤¤?¾?¹ç¡¢Mail¤«¤éÀ¸À®¤µ¤?¤?MimeMessage¤ÎMessage-Id¤Ë¤Ï
383 * <code>¥¿¥¤¥à¥¹¥¿¥ó¥× + ¥é¥ó¥À¥à¤ËÀ¸À®¤µ¤?¤?16·å¤Î¿ôÃÍ + ¤³¤³¤Ç¥»¥Ã¥È¤µ¤?¤¿ÃÍ</code>
384 * ¤¬»ÈÍѤµ¤?¤Þ¤¹¡£
385 * <p>
386 * À¸À®¤µ¤?¤?Message-Id¤ÎÎã¡£ (¼ÂºÝ¤Î¿ôÃÍÉôʬ¤ÏÁ÷¿®¥á¡¼¥?Ëè¤ËÊѤ?¤ê¤Þ¤¹)<ul>
387 * <li>messageId¤Ë'example.com'¤ò»ØÄꤷ¤¿¾?¹ç¡¦¡¦¡¦1095714924963.5619528074501343@example.com</li>
388 * <li>messageId¤Ë'@example.com'¤ò»ØÄꤷ¤¿¾?¹ç¡¦¡¦¡¦1095714924963.5619528074501343@example.com (¾å¤ÈƱ¤¸)</li>
389 * <li>messageId¤Ë'OML@example.com'¤ò»ØÄꤷ¤¿¾?¹ç¡¦¡¦¡¦1095714924963.5619528074501343.OML@example.com</li>
390 * <li>messageId¤Ë'.OML@example.com'¤ò»ØÄꤷ¤¿¾?¹ç¡¦¡¦¡¦1095714924963.5619528074501343.OML@example.com (¾å¤ÈƱ¤¸)</li>
391 * </ul>
392 * <p>
393 * <strong>Ã?:</strong> ¤³¤ÎMessage-Id¤Ï<code>send(Mail)</code>¤«<code>send(Mail[])</code>¥á¥½¥Ã¥É¤¬¸Æ¤Ó¤À¤?¤¿»?¤Ë¤Î¤ß͸ú¤Ç¤¹¡£MimeMessage¤òľÀÜÁ÷¿®¤¹¤?¾?¹ç¤Ë¤ÏŬÍѤµ¤?¤Þ¤»¤ó¡£
394 *
395 * @param messageId ¥á¡¼¥?¤ËÉÕ¤±¤é¤?¤?Message-Id¥Ø¥Ã¥À¤Î¥É¥á¥¤¥óÉôʬ
396 * @throws IllegalArgumentException @¤òÊ£¿ô´Þ¤ó¤Àʸ»úÎó¤ò»ØÄꤷ¤¿¾?¹?
397 */
398 public void setMessageId(String messageId) {
399 if (messageId == null || messageId.length() < 1) {
400 return;
401 }
402
403 String[] parts = messageId.split("@");
404 if (parts.length > 2) {
405 throw new IllegalArgumentException("messageId¥×¥úÁѥƥ£¤Ë'@'¤òÊ£¿ô´Þ¤à¤³¤È¤Ï¤Ç¤¤Þ¤»¤ó¡£[" + messageId
406 + "]");
407 }
408
409 this.messageId = messageId;
410 }
411 }