View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.net;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.commons.mail.Email;
25  import org.apache.commons.mail.EmailException;
26  import org.apache.commons.mail.HtmlEmail;
27  import org.apache.commons.mail.SimpleEmail;
28  import org.seasar.tuigwaa.system.TgwServiceException;
29  import org.seasar.tuigwaa.util.MimeMappings;
30  import org.seasar.tuigwaa.util.TgwResource;
31  
32  /***
33   * @author someda
34   */
35  public class EmailServiceImpl implements EmailService {
36  
37  	private Log log = LogFactory.getLog(getClass());
38  	
39  	private static Properties defaultProperties = new Properties();
40  	private static Map defaultHeaderMap = new HashMap();
41  	
42  	static{		
43  		String mailHost = TgwResource.getProperty("mail.host");
44  		String mailPort = TgwResource.getProperty("mail.port");
45  		String mailCharset = TgwResource.getProperty("mail.encode");
46  		String fromAddress = TgwResource.getProperty("mail.from.address");
47  		String fromPersonel = TgwResource.getProperty("mail.from.personel");
48  		String subjectPrefix = TgwResource.getProperty("mail.subject.prefix");		
49  		
50  		defaultProperties.setProperty(MAIL_HOST,mailHost);
51  		defaultProperties.setProperty(MAIL_PORT,mailPort);
52  		defaultProperties.setProperty(MAIL_CHARSET,mailCharset);
53  		defaultProperties.setProperty(MAIL_FROM_ADDRESS,fromAddress);
54  		defaultProperties.setProperty(MAIL_FROM_PERSONEL,fromPersonel);
55  		defaultProperties.setProperty(MAIL_SUBJECT,subjectPrefix);
56  		defaultProperties.setProperty(MAIL_REPLY_TO,fromAddress);
57  		
58  		defaultHeaderMap.put("Content-Transfer-Encoding","7bit");	
59  	}	
60  	
61  	public EmailServiceImpl(){
62  	}		
63  	
64  	public void sendEmail(String mailAddress, String mailContent, Properties mailProperties, Map headerMap) 
65  	throws TgwServiceException {
66  		
67  		Email mail = null;				
68  		try{
69  			mail = buildMail(mailProperties,headerMap);
70  			mail.addTo(mailAddress);
71  			setMailContent(mail,mailContent);
72  			mail.send();
73  			log.info("completed to send email to " + mailAddress);
74  		}catch(EmailException ee){
75  			log.error("failed to send email " + ee.getMessage());
76  			throw new TgwServiceException(ee);			
77  		}						
78  	}
79  	
80  	// ----- [private methods] -----		
81  	private void setMailContent(Email mail, String mailContent) throws EmailException{
82  		
83  		if(mail instanceof HtmlEmail){
84  			((HtmlEmail)mail).setHtmlMsg(mailContent);
85  		}else if(mail instanceof SimpleEmail){
86  			// due to bug of SimpleEmail#setMsg, not using the value of charset
87  			String contentType = MimeMappings.CONTENTTYPE_TXT + "; charset=" + defaultProperties.getProperty(MAIL_CHARSET);
88  			((SimpleEmail)mail).setContent(mailContent,contentType);
89  		}		
90  	}
91  	
92  	
93  	private Email buildMail(Properties mailProperties, Map headerMap) throws EmailException{
94  		
95  		Email mail = null;
96  		Properties sendProperties = mixSendProperties(mailProperties);
97  		Map sendHeaderMap = mixHeaderMap(headerMap);
98  		
99  		String contentType = sendProperties.getProperty(MAIL_CONTENTTYPE);
100 		if(contentType == null || MimeMappings.CONTENTTYPE_HTML.equals(contentType)){
101 			mail = new HtmlEmail();			
102 		}else if(MimeMappings.CONTENTTYPE_TXT.equals(contentType)){
103 			mail = new SimpleEmail();
104 		}
105 		
106 		mail.setHeaders(sendHeaderMap);
107 		
108 		mail.setHostName(sendProperties.getProperty(MAIL_HOST));
109 		mail.setCharset(sendProperties.getProperty(MAIL_CHARSET));
110 		mail.setSmtpPort(Integer.parseInt(sendProperties.getProperty(MAIL_PORT)));
111 		mail.setSubject(sendProperties.getProperty(MAIL_SUBJECT));
112 		mail.setFrom(sendProperties.getProperty(MAIL_FROM_ADDRESS),sendProperties.getProperty(MAIL_FROM_PERSONEL));
113 		mail.addReplyTo(sendProperties.getProperty(MAIL_REPLY_TO));
114 				
115 		return mail;
116 	}
117 	
118 	private Properties mixSendProperties(Properties mailProperties){
119 		
120 		if(mailProperties == null){
121 			return defaultProperties;
122 		}				
123 		Properties props = new Properties();
124 		props.putAll(defaultProperties);
125 		props.putAll(mailProperties);
126 		String fromAddress = mailProperties.getProperty(MAIL_FROM_ADDRESS);
127 		// overwrite reply-to of fromAddress
128 		if(fromAddress != null && ! "".equals(fromAddress)){
129 			props.setProperty(MAIL_REPLY_TO,fromAddress);
130 		}
131 		return props;
132 	}
133 	
134 	private Map mixHeaderMap(Map headerMap){		
135 		if(headerMap == null){
136 			return defaultHeaderMap;
137 		}
138 		
139 		Map map = new HashMap();
140 		map.putAll(defaultHeaderMap);
141 		map.putAll(headerMap);
142 		return map;
143 	}
144 	
145 }