/
MailService.java
240 lines (225 loc) · 11.8 KB
/
MailService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.service.impl;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.properties.TypedProperties;
import org.jumpmind.security.ISecurityService;
import org.jumpmind.security.SecurityConstants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.service.IMailService;
import org.jumpmind.symmetric.service.IParameterService;
public class MailService extends AbstractService implements IMailService {
protected ISecurityService securityService;
protected static final String JAVAMAIL_HOST_NAME = "mail.host";
protected static final String JAVAMAIL_TRANSPORT = "mail.transport";
protected static final String JAVAMAIL_PORT_NUMBER = "mail.smtp.port";
protected static final String JAVAMAIL_PORT_NUMBER_SSL = "mail.smtps.port";
protected static final String JAVAMAIL_FROM = "mail.from";
protected static final String JAVAMAIL_USE_STARTTLS = "mail.smtp.starttls.enable";
protected static final String JAVAMAIL_USE_AUTH = "mail.smtp.auth";
protected static final String JAVAMAIL_TRUST_HOST = "mail.smtp.ssl.trust";
protected static final String JAVAMAIL_TRUST_HOST_SSL = "mail.smtps.ssl.trust";
protected static final String JAVAMAIL_SOCKET_FACTORY_CLASS = "mail.smtp.socketFactory.class";
protected static final String JAVAMAIL_SSL_PROTOCOLS = "mail.smtp.ssl.protocols";
public MailService(IParameterService parameterService, ISecurityService securityService, ISymmetricDialect symmetricDialect) {
super(parameterService, symmetricDialect);
this.securityService = securityService;
}
public String sendEmail(String subject, String text, String toRecipients) {
return sendEmail(subject, text, toRecipients, null, null);
}
public String sendEmail(String subject, String text, String toRecipients, String ccRecipients, String bccRecipients) {
String password = decryptPassword(parameterService.getString(ParameterConstants.SMTP_PASSWORD));
return sendEmail(subject, text, toRecipients, ccRecipients, bccRecipients, getJavaMailProperties(),
parameterService.getString(ParameterConstants.SMTP_TRANSPORT, "smtp"),
parameterService.is(ParameterConstants.SMTP_USE_AUTH, false),
parameterService.getString(ParameterConstants.SMTP_USER), password);
}
public String sendEmail(String subject, String text, String toRecipients, TypedProperties prop) {
return sendEmail(subject, text, toRecipients, null, null, prop);
}
public String sendEmail(String subject, String text, String toRecipients, String ccRecipients, String bccRecipients, TypedProperties prop) {
return sendEmail(subject, text, toRecipients, ccRecipients, bccRecipients, getJavaMailProperties(prop),
prop.get(ParameterConstants.SMTP_TRANSPORT, "smtp"),
prop.is(ParameterConstants.SMTP_USE_AUTH, false),
prop.get(ParameterConstants.SMTP_USER), decryptPassword(prop.get(ParameterConstants.SMTP_PASSWORD)));
}
protected String sendEmail(String subject, String text, String toRecipients, String ccRecipients, String bccRecipients,
Properties prop, String transportType, boolean useAuth, String user, String password) {
Session session = Session.getInstance(prop);
ByteArrayOutputStream ba = null;
if (log.isDebugEnabled()) {
session.setDebug(true);
ba = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(ba);
session.setDebugOut(ps);
}
Transport transport;
try {
transport = session.getTransport(transportType);
} catch (NoSuchProviderException e) {
log.error("Failure while obtaining transport", e);
return getNestedErrorMessage(e);
}
try {
if (useAuth) {
transport.connect(user, decryptPassword(password));
} else {
transport.connect();
}
} catch (MessagingException e) {
log.error("Failure while connecting to transport", e);
return getNestedErrorMessage(e);
}
try {
MimeMessage message = new MimeMessage(session);
message.setSentDate(new Date());
if (StringUtils.isNotEmpty(toRecipients)) {
message.setRecipients(RecipientType.TO, toRecipients);
}
if (StringUtils.isNotEmpty(ccRecipients)) {
message.setRecipients(RecipientType.CC, ccRecipients);
}
if (StringUtils.isNotEmpty(bccRecipients)) {
message.setRecipients(RecipientType.BCC, bccRecipients);
}
message.setSubject(subject);
message.setText(text);
message.setFrom(new InternetAddress(prop.getProperty(JAVAMAIL_FROM)));
try {
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
log.error("Failure while sending notification", e);
return getNestedErrorMessage(e);
}
} catch (MessagingException e) {
log.error("Failure while preparing notification", e);
return e.getMessage();
} finally {
try {
transport.close();
} catch (MessagingException e) {
}
}
if (log.isDebugEnabled()) {
log.debug(ba.toString());
}
return null;
}
public String testTransport(TypedProperties prop) {
String error = null;
Transport transport = null;
try {
log.debug("SMTP parameters: {}", prop);
Properties javaMailProps = getJavaMailProperties(prop);
log.debug("JavaMail properties: {}", javaMailProps);
Session session = Session.getInstance(javaMailProps);
transport = session.getTransport(prop.get(ParameterConstants.SMTP_TRANSPORT, "smtp"));
if (prop.is(ParameterConstants.SMTP_USE_AUTH, false)) {
transport.connect(prop.get(ParameterConstants.SMTP_USER),
decryptPassword(prop.get(ParameterConstants.SMTP_PASSWORD)));
} else {
transport.connect();
}
} catch (NoSuchProviderException e) {
error = getNestedErrorMessage(e);
log.warn("Test of mail service failed:", e);
} catch (MessagingException e) {
error = getNestedErrorMessage(e);
log.warn("Test of mail service failed:", e);
} finally {
try {
if (transport != null) {
transport.close();
}
} catch (MessagingException e) {
}
}
return error;
}
protected String getNestedErrorMessage(Exception e) {
String error = e.getMessage();
Throwable e2 = e.getCause();
if (e2 != null) {
error += "\n" + e2.getMessage();
Throwable e3 = e2.getCause();
if (e3 != null) {
error += "\n" + e3.getMessage();
}
}
return error;
}
protected Properties getJavaMailProperties() {
Properties prop = new Properties();
prop.setProperty(JAVAMAIL_HOST_NAME, parameterService.getString(ParameterConstants.SMTP_HOST, "localhost"));
prop.setProperty(JAVAMAIL_PORT_NUMBER, parameterService.getString(ParameterConstants.SMTP_PORT, "25"));
prop.setProperty(JAVAMAIL_PORT_NUMBER_SSL, parameterService.getString(ParameterConstants.SMTP_PORT, "25"));
prop.setProperty(JAVAMAIL_FROM, parameterService.getString(ParameterConstants.SMTP_FROM, "root@localhost"));
prop.setProperty(JAVAMAIL_USE_STARTTLS, parameterService.getString(ParameterConstants.SMTP_USE_STARTTLS, "false"));
prop.setProperty(JAVAMAIL_USE_AUTH, parameterService.getString(ParameterConstants.SMTP_USE_AUTH, "false"));
prop.setProperty(JAVAMAIL_TRUST_HOST, parameterService.is(ParameterConstants.SMTP_ALLOW_UNTRUSTED_CERT, false) ? "*" : "");
prop.setProperty(JAVAMAIL_TRUST_HOST_SSL, parameterService.is(ParameterConstants.SMTP_ALLOW_UNTRUSTED_CERT, false) ? "*" : "");
prop.setProperty(JAVAMAIL_USE_AUTH, parameterService.getString(ParameterConstants.SMTP_USE_AUTH, "false"));
if (StringUtils.isNotBlank(parameterService.getString(ParameterConstants.SMTP_SOCKET_FACTORY_CLASS))) {
prop.setProperty(JAVAMAIL_SOCKET_FACTORY_CLASS, parameterService.getString(ParameterConstants.SMTP_SOCKET_FACTORY_CLASS));
}
if (StringUtils.isNotBlank(parameterService.getString(ParameterConstants.SMTP_SSL_PROTOCOLS))) {
prop.setProperty(JAVAMAIL_SSL_PROTOCOLS, parameterService.getString(ParameterConstants.SMTP_SSL_PROTOCOLS));
}
return prop;
}
protected Properties getJavaMailProperties(TypedProperties typedProp) {
Properties prop = new Properties();
prop.setProperty(JAVAMAIL_HOST_NAME, typedProp.get(ParameterConstants.SMTP_HOST, "localhost"));
prop.setProperty(JAVAMAIL_PORT_NUMBER, typedProp.get(ParameterConstants.SMTP_PORT, "25"));
prop.setProperty(JAVAMAIL_PORT_NUMBER_SSL, typedProp.get(ParameterConstants.SMTP_PORT, "25"));
prop.setProperty(JAVAMAIL_FROM, typedProp.get(ParameterConstants.SMTP_FROM, "root@localhost"));
prop.setProperty(JAVAMAIL_USE_STARTTLS, String.valueOf(typedProp.is(ParameterConstants.SMTP_USE_STARTTLS, false)));
prop.setProperty(JAVAMAIL_USE_AUTH, String.valueOf(typedProp.is(ParameterConstants.SMTP_USE_AUTH, false)));
prop.setProperty(JAVAMAIL_TRUST_HOST, typedProp.is(ParameterConstants.SMTP_ALLOW_UNTRUSTED_CERT, false) ? "*" : "");
prop.setProperty(JAVAMAIL_TRUST_HOST_SSL, typedProp.is(ParameterConstants.SMTP_ALLOW_UNTRUSTED_CERT, false) ? "*" : "");
if (StringUtils.isNotBlank(typedProp.get(ParameterConstants.SMTP_SOCKET_FACTORY_CLASS))) {
prop.setProperty(JAVAMAIL_SOCKET_FACTORY_CLASS, typedProp.get(ParameterConstants.SMTP_SOCKET_FACTORY_CLASS));
}
if (StringUtils.isNotBlank(typedProp.get(ParameterConstants.SMTP_SSL_PROTOCOLS))) {
prop.setProperty(JAVAMAIL_SSL_PROTOCOLS, typedProp.get(ParameterConstants.SMTP_SOCKET_FACTORY_CLASS));
}
return prop;
}
protected String decryptPassword(String password) {
if (password != null && password.startsWith(SecurityConstants.PREFIX_ENC)) {
return securityService.decrypt(password.substring(SecurityConstants.PREFIX_ENC.length()));
}
return password;
}
}