BrightSide Workbench Full Report + Source Code
MailMessage.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.mail.impl;
19 
20 import java.io.File;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import javax.naming.NamingException;
29 import org.amic.util.string.Strings;
30 import org.apache.commons.mail.Email;
31 import org.apache.commons.mail.EmailAttachment;
32 import org.apache.commons.mail.EmailException;
33 import org.apache.commons.mail.HtmlEmail;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.math.StringNumber;
36 
41 public class MailMessage {
42 
43  private final Map<String, File> imageMap = new HashMap<>();
44  private final Map<String, URL> imageURLMap = new HashMap<>();
45  private final Set<EmailAttachment> attachments = new HashSet<>();
46 
47  private String hostName, user, password, subject, message, port, tls, ssl;
48 
49  private HtmlEmail email = new HtmlEmail();
50 
51  public MailMessage() {
52  }
53 
54  public MailMessage(String hostName, String user, String password) {
55  this.hostName = hostName;
56  this.user = user;
57  this.password = password;
58  }
59 
60  public HtmlEmail getEmail() {
61  return email;
62  }
63 
64  public void setHostName(String hostName) {
65  this.hostName = hostName;
66  }
67 
68  public void setPassword(String password) {
69  this.password = password;
70  }
71 
72  public void setUser(String user) {
73  this.user = user;
74  }
75 
76  public String getMessage() {
77  return message;
78  }
79 
80  public void setMessage(String message) {
81  this.message = message;
82  }
83 
84  public String getSubject() {
85  return subject;
86  }
87 
88  public void setSubject(String subject) {
89  this.subject = subject;
90  }
91 
92  public String getPort() {
93  return port;
94  }
95 
96  public void setPort(String port) {
97  this.port = port;
98  }
99 
100  public String getTls() {
101  return tls;
102  }
103 
104  public void setTls(String tls) {
105  this.tls = tls;
106  }
107 
108  public String getSsl() {
109  return ssl;
110  }
111 
112  public void setSsl(String ssl) {
113  this.ssl = ssl;
114  }
115 
116  public void attachRelative(String path, String description, String name) {
117  EmailAttachment attachment = new EmailAttachment();
118  attachment.setPath(ElephantContext.getRealPath(path));
119  attachment.setDisposition(EmailAttachment.ATTACHMENT);
120  attachment.setDescription(description == null ? "" : description);
121  attachment.setName(name == null ? "" : name);
122  attachments.add(attachment);
123  }
124 
125  public void attach(String path, String description, String name) {
126  EmailAttachment attachment = new EmailAttachment();
127  attachment.setPath(path);
128  attachment.setDisposition(EmailAttachment.ATTACHMENT);
129  attachment.setDescription(description == null ? "" : description);
130  attachment.setName(name == null ? "" : name);
131  attachments.add(attachment);
132  }
133 
134  public void attachExternal(URL url, String description, String name) {
135  EmailAttachment attachment = new EmailAttachment();
136  attachment.setURL(url);
137  attachment.setDisposition(EmailAttachment.ATTACHMENT);
138  attachment.setDescription(description == null ? "" : description);
139  attachment.setName(name == null ? "" : name);
140  attachments.add(attachment);
141  }
142 
143  public void embedRelative(String name, String path) {
144  embed(name, new File(ElephantContext.getRealPath(path)));
145  }
146 
147  public void embed(String name, File file) {
148  imageMap.put(name, file);
149  }
150 
151  public void embedExternal(String name, String url) throws MalformedURLException {
152  imageURLMap.put(name, new URL(url));
153  }
154 
155  public void send() throws EmailException, MalformedURLException, NamingException {
156  email.setCharset(ElephantContext.getEncoding());
157  if (hostName.startsWith("java:")) {
158  email.setMailSessionFromJNDI(hostName);
159  } else {
160  email.setHostName(hostName);
161  }
162  if(StringNumber.toInteger(port) != null) {
163  email.setSmtpPort(StringNumber.toInteger(port));
164  }
165  if(StringNumber.toBoolean(ssl)) {
166  email.setSSLOnConnect(true);
167  if(StringNumber.toInteger(port) != null) {
168  email.setSslSmtpPort(port);
169  }
170  }
171  if(StringNumber.toBoolean(tls)) {
172  email.setStartTLSEnabled(true);
173  }
174  if(user != null && password != null) {
175  email.setAuthentication(user, password);
176  }
177 
178  for(String n : imageMap.keySet()) {
179  String cid = email.embed(imageMap.get(n), n);
180  message = message.replaceAll("image\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
181  }
182 
183  for(String n : imageURLMap.keySet()) {
184  String cid = email.embed(imageURLMap.get(n), n);
185  message = message.replaceAll("imageURL\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
186  }
187 
188  email.setSubject(subject);
189 
190  for(EmailAttachment ea : attachments) {
191  email.attach(ea);
192  }
193 
194  String serverBase = ElephantContext.getServerBase("http");
195  message = message.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
196  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
197  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
198  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
199 
200  message = new MailFormat().inline(message);
201 
202  email.setHtmlMsg(message);
203  email.setTextMsg("NO HTML SUPPORTED");
204 
205  email.send();
206  }
207 
208  public Email setFromIfNull(String email) throws EmailException {
209  if(this.email.getFromAddress() == null && !Strings.isBlank(email)) {
210  return setFrom(email);
211  }
212  return this.email;
213  }
214 
215  /* Delegates */
216 
217  public Date getSentDate() {
218  return email.getSentDate();
219  }
220 
221  public void setSentDate(Date date) {
222  email.setSentDate(date);
223  }
224 
225  public Email setFrom(String email, String name) throws EmailException {
226  return this.email.setFrom(email, name);
227  }
228 
229  public Email setFrom(String email) throws EmailException {
230  if(email != null) {
231  if(email.contains("|")) {
232  String[] parts = email.split("\\|");
233  return this.email.setFrom(parts[0], parts[1]);
234  }
235  return this.email.setFrom(email);
236  }
237  return this.email;
238  }
239 
240  public Email addTo(String email, String name) throws EmailException {
241  if(Strings.isBlank(email)) return null;
242  return this.email.addTo(email, name);
243  }
244 
245  public Email addTo(String email) throws EmailException {
246  if(Strings.isBlank(email)) return null;
247  return this.email.addTo(email);
248  }
249 
250  public Email addReplyTo(String email, String name) throws EmailException {
251  return this.email.addReplyTo(email, name);
252  }
253 
254  public Email addReplyTo(String email) throws EmailException {
255  return this.email.addReplyTo(email);
256  }
257 
258  public void addHeader(String name, String value) {
259  email.addHeader(name, value);
260  }
261 
262  public Email addCc(String email, String name) throws EmailException {
263  return this.email.addCc(email, name);
264  }
265 
266  public Email addCc(String email) throws EmailException {
267  return this.email.addCc(email);
268  }
269 
270  public Email addBcc(String email, String name) throws EmailException {
271  return this.email.addBcc(email, name);
272  }
273 
274  public Email addBcc(String email) throws EmailException {
275  return this.email.addBcc(email);
276  }
277 
278  public void setCharset(String newCharset) {
279  email.setCharset(newCharset);
280  }
281 
282 }
static String getServerBase(String scheme)
String inline(String html)
Definition: MailFormat.java:56
Email addReplyTo(String email)
void embedExternal(String name, String url)
void attachRelative(String path, String description, String name)
void setSubject(String subject)
void setHostName(String hostName)
void attachExternal(URL url, String description, String name)
void attach(String path, String description, String name)
void addHeader(String name, String value)
void setCharset(String newCharset)
void setPassword(String password)
Email setFromIfNull(String email)
Email addCc(String email, String name)
Email addReplyTo(String email, String name)
Email setFrom(String email, String name)
void setMessage(String message)
Email addTo(String email, String name)
MailMessage(String hostName, String user, String password)
void embedRelative(String name, String path)
void embed(String name, File file)
Email addBcc(String email, String name)
static Integer toInteger(String value)
static Boolean toBoolean(String value)