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 package info.magnolia.module.mail;
35
36 import info.magnolia.module.mail.templates.MailAttachment;
37 import info.magnolia.module.mail.util.MailUtil;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Properties;
44
45 import javax.mail.Authenticator;
46 import javax.mail.PasswordAuthentication;
47 import javax.mail.Session;
48
49 import org.apache.commons.collections.CollectionUtils;
50 import org.apache.commons.lang.StringUtils;
51
52
53
54
55
56 public class MailTemplate {
57
58 public static String MAIL_CONTENT_TYPE = "contentType";
59
60 public static String MAIL_FROM = "from";
61
62 public static String MAIL_SUBJECT = "subject";
63
64 public static String MAIL_TO = "to";
65
66 public static String MAIL_TO_WORKFLOW = "mailTo";
67
68 public static String MAIL_CC = "cc";
69
70 public static String MAIL_TYPE = "type";
71
72 public static String MAIL_PARAMETERS = "parameters";
73
74 public static String MAIL_ATTACHMENTS = "attachments";
75
76 public static String MAIL_BODY = "body";
77
78 public static final String MAIL_HTML = "html";
79
80 public static final String MAIL_TEMPLATE_FILE = "templateFile";
81
82 private static final String MAIL_REPLY_TO = "replyTo";
83
84 private static final String MAIL_BCC = "bcc";
85
86 private Map<String, Object> parameters = new HashMap<String, Object>();
87
88 private List<MailAttachment> attachments = new ArrayList<MailAttachment>();
89
90 private String from;
91
92 private String to;
93
94 private String cc;
95
96 private String subject;
97
98 private String type;
99
100 private String contentType;
101
102 private String name;
103
104 private String text;
105
106 private String templateFile;
107
108 private String bcc;
109
110 private String replyTo;
111
112 public MailTemplate() {
113
114 }
115
116 public Map<String, Object> getParameters() {
117
118 return new HashMap<String, Object>(parameters);
119 }
120
121 public void setParameters(Map<String, Object> parameters) {
122
123 this.parameters.clear();
124 this.parameters.putAll(parameters);
125 }
126
127 public List<MailAttachment> getAttachments() {
128
129 return new ArrayList<MailAttachment>(attachments);
130 }
131
132 public void setAttachments(List<MailAttachment> attachments) {
133
134 this.attachments.clear();
135 this.attachments.addAll(attachments);
136 }
137
138 public void addAttachment(MailAttachment attachment) {
139
140 this.attachments.add(attachment);
141 }
142
143 public String getFrom() {
144 return from;
145 }
146
147 public void setFrom(String from) {
148 this.from = from;
149 }
150
151 public String getTo() {
152 return to;
153 }
154
155 public void setTo(String to) {
156 this.to = to;
157 }
158
159 public String getSubject() {
160 return subject;
161 }
162
163 public void setSubject(String subject) {
164 this.subject = subject;
165 }
166
167 public String getType() {
168 return type;
169 }
170
171 public void setType(String type) {
172 this.type = type;
173 }
174
175 public String getContentType() {
176 return contentType;
177 }
178
179 public void setContentType(String contentType) {
180 this.contentType = contentType;
181 }
182
183 public String getName() {
184 return name;
185 }
186
187 public void setName(String name) {
188 this.name = name;
189 }
190
191 public String getText() {
192 return text;
193 }
194
195 public void setText(String text) {
196 this.text = text;
197 }
198
199
200 public String getCc() {
201 return cc;
202 }
203
204
205 public void setCc(String cc) {
206 this.cc = cc;
207 }
208
209 public void setValues(Map<String, Object> params, List<MailAttachment> attachments) {
210
211 if(params.containsKey(MAIL_TEMPLATE_FILE)) {
212 this.templateFile = (String) params.get(MAIL_TEMPLATE_FILE);
213 }
214
215 if(params.containsKey(MAIL_CONTENT_TYPE)) {
216 this.contentType = (String) params.get(MAIL_CONTENT_TYPE);
217 }
218
219 if(params.containsKey(MAIL_FROM)) {
220 this.from = (String) params.get(MAIL_FROM);
221 }
222
223 if(params.containsKey(MAIL_SUBJECT)) {
224 this.subject = (String) params.get(MAIL_SUBJECT);
225 }
226
227 if(params.containsKey(MAIL_TO)) {
228 this.to = (String) params.get(MAIL_TO);
229 }
230 if(params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
231 this.to = (String) params.get(MailTemplate.MAIL_TO_WORKFLOW);
232 }
233
234 if(params.containsKey(MAIL_CC)) {
235 this.cc = (String) params.get(MAIL_CC);
236 }
237
238 if(params.containsKey(MAIL_TYPE)) {
239 this.type = (String) params.get(MAIL_TYPE);
240 }
241
242 if(params.containsKey(MAIL_BODY)) {
243 this.text = (String) params.get(MAIL_BODY);
244 }
245
246 if(params.containsKey(MAIL_REPLY_TO)) {
247 this.replyTo = (String) params.get(MAIL_REPLY_TO);
248 }
249
250 if(params.containsKey(MAIL_BCC)) {
251 this.bcc = (String) params.get(MAIL_BCC);
252 }
253
254
255 this.parameters.clear();
256 this.parameters.putAll(params);
257
258 if(!CollectionUtils.isEmpty(attachments)) {
259
260 this.attachments.clear();
261 this.attachments.addAll(attachments);
262 }
263 }
264
265 public String getTemplateFile() {
266 return templateFile;
267 }
268
269 public void setTemplateFile(String templateFile) {
270 this.templateFile = templateFile;
271 }
272
273 public String getBcc() {
274 return bcc;
275 }
276
277 public void setBcc(String bcc) {
278 this.bcc = bcc;
279 }
280
281 public String getReplyTo() {
282 return replyTo;
283 }
284
285 public void setReplyTo(String replyTo) {
286 this.replyTo = replyTo;
287 }
288
289 public Session initSession() {
290
291 Map<String, String> smtp = MailModule.getInstance().getSmtp();
292 Properties props = new Properties();
293
294 props.put("mail.smtp.host", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SERVER, smtp.get(MailConstants.SMTP_SERVER)));
295 props.put("mail.smtp.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
296
297 final String starttls = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_TLS, smtp.get(MailConstants.SMTP_TLS));
298 if ("true".equals(starttls)) {
299
300 props.put("mail.smtp.starttls.enable", starttls);
301 }
302 final String ssl = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_SSL, smtp.get(MailConstants.SMTP_SSL));
303 if ("true".equals(ssl)) {
304
305 props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
306 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
307 props.put("mail.smtp.socketFactory.fallback", "false");
308 }
309
310 Authenticator auth = null;
311 final String smtpUser = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, smtp.get(MailConstants.SMTP_USER));
312 final String smtpPassword = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_PASSWORD, smtp.get(MailConstants.SMTP_PASSWORD));
313 if (StringUtils.isNotBlank(smtpUser)) {
314 props.put("mail.smtp.auth", "true");
315 props.put("mail.smtp.user", smtpUser);
316 auth = new Authenticator() {
317 @Override
318 protected PasswordAuthentication getPasswordAuthentication() {
319 return new PasswordAuthentication(smtpUser, smtpPassword);
320 }
321 };
322 }
323 props.put("mail.smtp.sendpartial", StringUtils.defaultString(smtp.get(MailConstants.SMTP_SEND_PARTIAL)));
324 return Session.getInstance(props, auth);
325 }
326
327 }