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.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;
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<>();
48 private String hostName, user, password, subject, message, port, tls, ssl;
50 private HtmlEmail email =
new HtmlEmail();
55 public MailMessage(String hostName, String user, String password) {
56 this.hostName = hostName;
58 this.password = password;
66 this.hostName = hostName;
70 this.password = password;
82 this.message = message;
90 this.subject = subject;
118 EmailAttachment attachment =
new EmailAttachment();
120 attachment.setDisposition(EmailAttachment.ATTACHMENT);
121 attachment.setDescription(description ==
null ?
"" : description);
122 attachment.setName(name ==
null ?
"" : name);
123 attachments.add(attachment);
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);
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);
148 public void embed(String name, File file) {
149 imageMap.put(name, file);
152 public void embedExternal(String name, String url)
throws MalformedURLException {
153 imageURLMap.put(name,
new URL(url));
156 public void send() throws EmailException, MalformedURLException, NamingException {
158 if (hostName.startsWith(
"java:")) {
159 email.setMailSessionFromJNDI(hostName);
161 email.setHostName(hostName);
167 email.setSSLOnConnect(
true);
169 email.setSslSmtpPort(port);
173 email.setStartTLSEnabled(
true);
175 if(user !=
null && password !=
null) {
176 email.setAuthentication(user, password);
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 +
"\"/>");
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 +
"\"/>");
189 email.setSubject(subject);
191 for(EmailAttachment ea : attachments) {
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\\\"");
203 email.setHtmlMsg(message);
204 email.setTextMsg(
"NO HTML SUPPORTED");
210 if(this.email.getFromAddress() ==
null && !Strings.isBlank(email)) {
219 return email.getSentDate();
223 email.setSentDate(date);
226 public Email
setFrom(String email, String name)
throws EmailException {
227 return this.email.setFrom(email, name);
230 public Email
setFrom(String email)
throws EmailException {
232 if(email.contains(
"|")) {
233 String[] parts = email.split(
"\\|");
234 return this.email.setFrom(parts[0], parts[1]);
236 return this.email.setFrom(email);
241 public Email
addTo(String email, String name)
throws EmailException {
242 if(Strings.isBlank(email))
return null;
243 return this.email.addTo(email, name);
246 public Email
addTo(String email)
throws EmailException {
247 if(Strings.isBlank(email))
return null;
248 return this.email.addTo(email);
251 public Email
addReplyTo(String email, String name)
throws EmailException {
252 return this.email.addReplyTo(email, name);
255 public Email
addReplyTo(String email)
throws EmailException {
256 return this.email.addReplyTo(email);
260 email.addHeader(name, value);
263 public Email
addCc(String email, String name)
throws EmailException {
264 return this.email.addCc(email, name);
267 public Email
addCc(String email)
throws EmailException {
268 return this.email.addCc(email);
271 public Email
addBcc(String email, String name)
throws EmailException {
272 return this.email.addBcc(email, name);
275 public Email
addBcc(String email)
throws EmailException {
276 return this.email.addBcc(email);
280 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)