MailHelper.java
3.93 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.mailer.MailService
* com.day.cq.mailer.MailingException
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.text.StrSubstitutor
* org.apache.commons.mail.Email
* org.apache.commons.mail.EmailException
* org.apache.commons.mail.SimpleEmail
* org.apache.sling.api.resource.LoginException
*/
package com.day.cq.dam.core.impl;
import com.day.cq.mailer.MailService;
import com.day.cq.mailer.MailingException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.sling.api.resource.LoginException;
public class MailHelper {
private static final String EMAIL_FOOTER = "footer";
private static final String EMAIL_MESSAGE = "message";
private static final String EMAIL_HEADER = "header";
private static final String EMAIL_SUBJECT = "subject";
public static void sendMail(Session adminSession, MailService mailer, Map<String, String> sendTo, Map<String, String> parameters) throws RepositoryException, IOException, LoginException, EmailException, MailingException {
String templateRootPath = parameters.get("templateRootPath");
Node node = adminSession.getNode(templateRootPath);
InputStream is = node.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream();
Properties properties = new Properties();
properties.load(is);
SimpleEmail email = null;
email = new SimpleEmail();
email.setCharset("UTF-8");
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", "text/plain; charset=utf-8");
email.setHeaders(headerMap);
email.setSubject(MailHelper.getSubject(properties, parameters, ""));
for (Map.Entry<String, String> entry : sendTo.entrySet()) {
email.addTo(entry.getValue());
email.setMsg(MailHelper.getMessage(properties, parameters, entry.getKey()));
if (mailer != null) {
mailer.send((Object)email);
}
email.getToAddresses().clear();
}
}
private static String getSubject(Properties properties, Map<String, String> parameters, String invitee) throws RepositoryException {
Properties values = MailHelper.getValues(parameters, invitee);
String subject = StrSubstitutor.replace(properties.get("subject"), (Map)values);
return subject;
}
private static String getMessage(Properties properties, Map<String, String> parameters, String invitee) throws RepositoryException {
Properties values = MailHelper.getValues(parameters, invitee);
StringBuilder msg = new StringBuilder();
msg.append(StrSubstitutor.replace(properties.get("header"), (Map)values));
msg.append(StrSubstitutor.replace(properties.get("message"), (Map)values));
msg.append(StrSubstitutor.replace(properties.get("footer"), (Map)values));
return msg.toString();
}
private static Properties getValues(Map<String, String> parameters, String invitee) throws RepositoryException {
Properties values = new Properties();
for (Map.Entry<String, String> entry : parameters.entrySet()) {
values.setProperty(entry.getKey(), entry.getValue());
}
values.setProperty("inviteeFirstName", invitee);
values.setProperty("time", new Date().toString());
return values;
}
}