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