BrightSide Workbench Full Report + Source Code
elephant-mail/src/main/java/org/turro/mail/message/MailMessage.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.mail.message;
20 
21 import java.io.File;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29 import javax.naming.NamingException;
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.mail.provider.MailProvider;
36 import org.turro.string.Strings;
37 
42 public class MailMessage {
43 
44  private final HtmlEmail email = new HtmlEmail();
45 
46  private final Map<String, File> imageMap = new HashMap<>();
47  private final Map<String, URL> imageURLMap = new HashMap<>();
48  private final Set<EmailAttachment> attachments = new HashSet<>();
49 
50  private String subject, message;
51 
52  public HtmlEmail getEmail() {
53  return email;
54  }
55 
56  public String getMessage() {
57  return message;
58  }
59 
60  public void setMessage(String message) {
61  this.message = message;
62  }
63 
64  public String getSubject() {
65  return subject;
66  }
67 
68  public void setSubject(String subject) {
69  this.subject = subject;
70  }
71 
73  return provider;
74  }
75 
76  public void attachRelative(String path, String description, String name) {
77  EmailAttachment attachment = new EmailAttachment();
78  attachment.setPath(ElephantContext.getRealPath(path));
79  attachment.setDisposition(EmailAttachment.ATTACHMENT);
80  attachment.setDescription(description == null ? "" : description);
81  attachment.setName(name == null ? "" : name);
82  attachments.add(attachment);
83  }
84 
85  public void attach(String path, String description, String name) {
86  EmailAttachment attachment = new EmailAttachment();
87  attachment.setPath(path);
88  attachment.setDisposition(EmailAttachment.ATTACHMENT);
89  attachment.setDescription(description == null ? "" : description);
90  attachment.setName(name == null ? "" : name);
91  attachments.add(attachment);
92  }
93 
94  public void attachExternal(URL url, String description, String name) {
95  EmailAttachment attachment = new EmailAttachment();
96  attachment.setURL(url);
97  attachment.setDisposition(EmailAttachment.ATTACHMENT);
98  attachment.setDescription(description == null ? "" : description);
99  attachment.setName(name == null ? "" : name);
100  attachments.add(attachment);
101  }
102 
103  public void embedRelative(String name, String path) {
104  embed(name, new File(ElephantContext.getRealPath(path)));
105  }
106 
107  public void embed(String name, File file) {
108  imageMap.put(name, file);
109  }
110 
111  public void embedExternal(String name, String url) throws MalformedURLException {
112  imageURLMap.put(name, new URL(url));
113  }
114 
115  public void send() throws EmailException, MalformedURLException, NamingException {
116  provider.setupMail(email);
117 
118  for(String n : imageMap.keySet()) {
119  String cid = email.embed(imageMap.get(n), n);
120  message = message.replaceAll("image\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
121  }
122 
123  for(String n : imageURLMap.keySet()) {
124  String cid = email.embed(imageURLMap.get(n), n);
125  message = message.replaceAll("imageURL\\(" + n + "\\)", "<img src=\"cid:" + cid + "\"/>");
126  }
127 
128  email.setSubject(subject);
129 
130  for(EmailAttachment ea : attachments) {
131  email.attach(ea);
132  }
133 
134  String serverBase = ElephantContext.getServerBase("http");
135  message = message.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
136  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
137  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
138  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
139 
140  message = new MailInline().inline(message);
141 
142  email.setHtmlMsg(message);
143  email.setTextMsg("NO HTML SUPPORTED");
144 
145  email.send();
146  }
147 
148  /* Delegates */
149 
150  public Date getSentDate() {
151  return email.getSentDate();
152  }
153 
154  public void setSentDate(Date date) {
155  email.setSentDate(date);
156  }
157 
158  public Email setFrom(String email, String name) throws EmailException {
159  return this.email.setFrom(email, name, ElephantContext.getEncoding());
160  }
161 
162  public Email setFrom(String email) throws EmailException {
163  if(email != null) {
164  if(email.contains("|")) {
165  String[] parts = email.split("\\|");
166  return this.email.setFrom(parts[0], parts[1], ElephantContext.getEncoding());
167  }
168  return this.email.setFrom(email);
169  }
170  return this.email;
171  }
172 
173  public Email addTo(String email, String name) throws EmailException {
174  if(Strings.isBlank(email)) return null;
175  return this.email.addTo(email, name, ElephantContext.getEncoding());
176  }
177 
178  public Email addTo(String email) throws EmailException {
179  if(Strings.isBlank(email)) return null;
180  return this.email.addTo(email);
181  }
182 
183  public Email addReplyTo(String email, String name) throws EmailException {
184  return this.email.addReplyTo(email, name, ElephantContext.getEncoding());
185  }
186 
187  public Email addReplyTo(String email) throws EmailException {
188  return this.email.addReplyTo(email);
189  }
190 
191  public void addHeader(String name, String value) {
192  email.addHeader(name, value);
193  }
194 
195  public Email addCc(String email, String name) throws EmailException {
196  return this.email.addCc(email, name, ElephantContext.getEncoding());
197  }
198 
199  public Email addCc(String email) throws EmailException {
200  return this.email.addCc(email);
201  }
202 
203  public Email addBcc(String email, String name) throws EmailException {
204  return this.email.addBcc(email, name, ElephantContext.getEncoding());
205  }
206 
207  public Email addBcc(String email) throws EmailException {
208  return this.email.addBcc(email);
209  }
210 
211  /* Factory */
212 
214  return new MailMessage(provider);
215  }
216 
217  protected final MailProvider provider;
218 
220  this.provider = provider;
221  email.setCharset(ElephantContext.getEncoding());
222  }
223 
224 }
static String getServerBase(String scheme)