18 package org.turro.mail.impl;
21 import java.net.MalformedURLException;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.HashSet;
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;
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<>();
47 private String hostName, user, password, subject, message, port, tls, ssl;
49 private HtmlEmail email =
new HtmlEmail();
54 public MailMessage(String hostName, String user, String password) {
55 this.hostName = hostName;
57 this.password = password;
65 this.hostName = hostName;
69 this.password = password;
81 this.message = message;
89 this.subject = subject;
117 EmailAttachment attachment =
new EmailAttachment();
119 attachment.setDisposition(EmailAttachment.ATTACHMENT);
120 attachment.setDescription(description ==
null ?
"" : description);
121 attachment.setName(name ==
null ?
"" : name);
122 attachments.add(attachment);
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);
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);
147 public void embed(String name, File file) {
148 imageMap.put(name, file);
151 public void embedExternal(String name, String url)
throws MalformedURLException {
152 imageURLMap.put(name,
new URL(url));
155 public void send() throws EmailException, MalformedURLException, NamingException {
157 if (hostName.startsWith(
"java:")) {
158 email.setMailSessionFromJNDI(hostName);
160 email.setHostName(hostName);
166 email.setSSLOnConnect(
true);
168 email.setSslSmtpPort(port);
172 email.setStartTLSEnabled(
true);
174 if(user !=
null && password !=
null) {
175 email.setAuthentication(user, password);
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 +
"\"/>");
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 +
"\"/>");
188 email.setSubject(subject);
190 for(EmailAttachment ea : attachments) {
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\\\"");
202 email.setHtmlMsg(message);
203 email.setTextMsg(
"NO HTML SUPPORTED");
209 if(this.email.getFromAddress() ==
null && !Strings.isBlank(email)) {
218 return email.getSentDate();
222 email.setSentDate(date);
225 public Email
setFrom(String email, String name)
throws EmailException {
226 return this.email.setFrom(email, name);
229 public Email
setFrom(String email)
throws EmailException {
231 if(email.contains(
"|")) {
232 String[] parts = email.split(
"\\|");
233 return this.email.setFrom(parts[0], parts[1]);
235 return this.email.setFrom(email);
240 public Email
addTo(String email, String name)
throws EmailException {
241 if(Strings.isBlank(email))
return null;
242 return this.email.addTo(email, name);
245 public Email
addTo(String email)
throws EmailException {
246 if(Strings.isBlank(email))
return null;
247 return this.email.addTo(email);
250 public Email
addReplyTo(String email, String name)
throws EmailException {
251 return this.email.addReplyTo(email, name);
254 public Email
addReplyTo(String email)
throws EmailException {
255 return this.email.addReplyTo(email);
259 email.addHeader(name, value);
262 public Email
addCc(String email, String name)
throws EmailException {
263 return this.email.addCc(email, name);
266 public Email
addCc(String email)
throws EmailException {
267 return this.email.addCc(email);
270 public Email
addBcc(String email, String name)
throws EmailException {
271 return this.email.addBcc(email, name);
274 public Email
addBcc(String email)
throws EmailException {
275 return this.email.addBcc(email);
279 email.setCharset(newCharset);
static String getRealPath(String path)
static String getServerBase(String scheme)
static String getEncoding()
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)
Email addTo(String email)
void setSentDate(Date date)
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 addCc(String email)
Email setFromIfNull(String email)
void setPort(String port)
Email addBcc(String email)
Email addCc(String email, String name)
Email addReplyTo(String email, String name)
Email setFrom(String email, String name)
void setMessage(String message)
void setUser(String user)
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)
Email setFrom(String email)
static Integer toInteger(String value)
static Boolean toBoolean(String value)