View Javadoc

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  		// ¥°¥ú½¼¥Ð¥?Return-Path¤ÎÀßÄ?
89  		putOnReturnPath(this.returnPath);
90  
91  		try {
92  			// SMTP¥µ¡¼¥Ð¤ËÀܳ
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 				// SMTP¥µ¡¼¥Ð¤È¤ÎÀܳ¤òÀÚÃÇ
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 				// ¥°¥ú½¼¥Ð¥?Return-Path¤Î²ò½?
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 		// MimeMessage¤ÎÀ¸À®
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 }