BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/mail/impl/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.turro.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 @Deprecated
42 public class MailMessage {
43 
44  private final Map<String, File> imageMap = new HashMap<>();
45  private final Map<String, URL> imageURLMap = new HashMap<>();
46  private final Set<EmailAttachment> attachments = new HashSet<>();
47 
48  private String hostName, user, password, subject, message, port, tls, ssl;
49 
50  private HtmlEmail email = new HtmlEmail();
51 
52  public MailMessage() {
53  }
54 
55  public MailMessage(String hostName, String user, String password) {
56  this.hostName = hostName;
57  this.user = user;
58  this.password = password;
59  }
60 
61  public HtmlEmail getEmail() {
62  return email;
63  }
64 
65  public void setHostName(String hostName) {
66  this.hostName = hostName;
67  }
68 
69  public void setPassword(String password) {
70  this.password = password;
71  }
72 
73  public void setUser(String user) {
74  this.user = user;
75  }
76 
77  public String getMessage() {
78  return message;
79  }
80 
81  public void setMessage(String message) {
82  this.message = message;
83  }
84 
85  public String getSubject() {
86  return subject;
87  }
88 
89  public void setSubject(String subject) {
90  this.subject = subject;
91  }
92 
93  public String getPort() {
94  return port;
95  }
96 
97  public void setPort(String port) {
98  this.port = port;
99  }
100 
101  public String getTls() {
102  return tls;
103  }
104 
105  public void setTls(String tls) {
106  this.tls = tls;
107  }
108 
109  public String getSsl() {
110  return ssl;
111  }
112 
113  public void setSsl(String ssl) {
114  this.ssl = ssl;
115  }
116 
117  public void attachRelative(String path, String description, String name) {
118  EmailAttachment attachment = new EmailAttachment();
119  attachment.setPath(ElephantContext.getRealPath(path));
120  attachment.setDisposition(EmailAttachment.ATTACHMENT);
121  attachment.setDescription(description == null ? "" : description);
122  attachment.setName(name == null ? "" : name);
123  attachments.add(attachment);
124  }
125 
126  public void attach(String path, String description, String name) {
127  EmailAttachment attachment = new EmailAttachment();
128  attachment.setPath(path);
129  attachment.setDisposition(EmailAttachment.ATTACHMENT);
130  attachment.setDescription(description == null ? "" : description);
131  attachment.setName(name == null ? "" : name);
132  attachments.add(attachment);
133  }
134 
135  public void attachExternal(URL url, String description, String name) {
136  EmailAttachment attachment = new EmailAttachment();
137  attachment.setURL(url);
138  attachment.setDisposition(EmailAttachment.ATTACHMENT);
139  attachment.setDescription(description == null ? "" : description);
140  attachment.setName(name == null ? "" : name);
141  attachments.add(attachment);
142  }
143 
144  public void embedRelative(String name, String path) {
145  embed(name, new File(ElephantContext.getRealPath(path)));
146  }
147 
148  public void embed(String name, File file) {
149  imageMap.put(name, file);
150  }
151 
152  public void embedExternal(String name, String url) throws MalformedURLException {
153  imageURLMap.put(name, new URL(url));
154  }
155 
156  public void send() throws EmailException, MalformedURLException, NamingException {
157  email.setCharset(ElephantContext.getEncoding());
158  if (hostName.startsWith("java:")) {
159  email.setMailSessionFromJNDI(hostName);
160  } else {
161  email.setHostName(hostName);
162  }
163  if(StringNumber.toInteger(port) != null) {
164  email.setSmtpPort(StringNumber.toInteger(port));
165  }
166  if(StringNumber.toBoolean(ssl)) {
167  email.setSSLOnConnect(true);
168  if(StringNumber.toInteger(port) != null) {
169  email.setSslSmtpPort(port);
170  }
171  }
172  if(StringNumber.toBoolean(tls)) {
173  email.setStartTLSEnabled(true);
174  }
175  if(user != null && password != null) {
176  email.setAuthentication(user, password);
177  }
178 
179  for(String n : imageMap.keySet()) {
180  String cid = email.embed(imageMap.get(n), n);
181  message = message.replaceAll("image\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
182  }
183 
184  for(String n : imageURLMap.keySet()) {
185  String cid = email.embed(imageURLMap.get(n), n);
186  message = message.replaceAll("imageURL\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
187  }
188 
189  email.setSubject(subject);
190 
191  for(EmailAttachment ea : attachments) {
192  email.attach(ea);
193  }
194 
195  String serverBase = ElephantContext.getServerBase("http");
196  message = message.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
197  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
198  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
199  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
200 
201  message = new MailFormat().inline(message);
202 
203  email.setHtmlMsg(message);
204  email.setTextMsg("NO HTML SUPPORTED");
205 
206  email.send();
207  }
208 
209  public Email setFromIfNull(String email) throws EmailException {
210  if(this.email.getFromAddress() == null && !Strings.isBlank(email)) {
211  return setFrom(email);
212  }
213  return this.email;
214  }
215 
216  /* Delegates */
217 
218  public Date getSentDate() {
219  return email.getSentDate();
220  }
221 
222  public void setSentDate(Date date) {
223  email.setSentDate(date);
224  }
225 
226  public Email setFrom(String email, String name) throws EmailException {
227  return this.email.setFrom(email, name);
228  }
229 
230  public Email setFrom(String email) throws EmailException {
231  if(email != null) {
232  if(email.contains("|")) {
233  String[] parts = email.split("\\|");
234  return this.email.setFrom(parts[0], parts[1]);
235  }
236  return this.email.setFrom(email);
237  }
238  return this.email;
239  }
240 
241  public Email addTo(String email, String name) throws EmailException {
242  if(Strings.isBlank(email)) return null;
243  return this.email.addTo(email, name);
244  }
245 
246  public Email addTo(String email) throws EmailException {
247  if(Strings.isBlank(email)) return null;
248  return this.email.addTo(email);
249  }
250 
251  public Email addReplyTo(String email, String name) throws EmailException {
252  return this.email.addReplyTo(email, name);
253  }
254 
255  public Email addReplyTo(String email) throws EmailException {
256  return this.email.addReplyTo(email);
257  }
258 
259  public void addHeader(String name, String value) {
260  email.addHeader(name, value);
261  }
262 
263  public Email addCc(String email, String name) throws EmailException {
264  return this.email.addCc(email, name);
265  }
266 
267  public Email addCc(String email) throws EmailException {
268  return this.email.addCc(email);
269  }
270 
271  public Email addBcc(String email, String name) throws EmailException {
272  return this.email.addBcc(email, name);
273  }
274 
275  public Email addBcc(String email) throws EmailException {
276  return this.email.addBcc(email);
277  }
278 
279  public void setCharset(String newCharset) {
280  email.setCharset(newCharset);
281  }
282 
283 }
static String getServerBase(String scheme)
String inline(String html)
Definition: MailFormat.java:56
void attachRelative(String path, String description, String name)
void attachExternal(URL url, String description, String name)
void attach(String path, String description, String name)
MailMessage(String hostName, String user, String password)
static Integer toInteger(String value)
static Boolean toBoolean(String value)