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