19 package org.turro.mail.message;
22 import java.net.MalformedURLException;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.HashSet;
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;
44 private final HtmlEmail email =
new HtmlEmail();
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<>();
50 private String subject, message;
61 this.message = message;
69 this.subject = subject;
77 EmailAttachment attachment =
new EmailAttachment();
79 attachment.setDisposition(EmailAttachment.ATTACHMENT);
80 attachment.setDescription(description ==
null ?
"" : description);
81 attachment.setName(name ==
null ?
"" : name);
82 attachments.add(attachment);
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);
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);
107 public void embed(String name, File file) {
108 imageMap.put(name, file);
111 public void embedExternal(String name, String url)
throws MalformedURLException {
112 imageURLMap.put(name,
new URL(url));
115 public void send() throws EmailException, MalformedURLException, NamingException {
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 +
"\"/>");
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 +
"\"/>");
128 email.setSubject(subject);
130 for(EmailAttachment ea : attachments) {
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\\\"");
142 email.setHtmlMsg(message);
143 email.setTextMsg(
"NO HTML SUPPORTED");
151 return email.getSentDate();
155 email.setSentDate(date);
158 public Email
setFrom(String email, String name)
throws EmailException {
162 public Email
setFrom(String email)
throws EmailException {
164 if(email.contains(
"|")) {
165 String[] parts = email.split(
"\\|");
168 return this.email.setFrom(email);
173 public Email
addTo(String email, String name)
throws EmailException {
174 if(Strings.isBlank(email))
return null;
178 public Email
addTo(String email)
throws EmailException {
179 if(Strings.isBlank(email))
return null;
180 return this.email.addTo(email);
183 public Email
addReplyTo(String email, String name)
throws EmailException {
187 public Email
addReplyTo(String email)
throws EmailException {
188 return this.email.addReplyTo(email);
192 email.addHeader(name, value);
195 public Email
addCc(String email, String name)
throws EmailException {
199 public Email
addCc(String email)
throws EmailException {
200 return this.email.addCc(email);
203 public Email
addBcc(String email, String name)
throws EmailException {
207 public Email
addBcc(String email)
throws EmailException {
208 return this.email.addBcc(email);
static String getRealPath(String path)
static String getServerBase(String scheme)
static String getEncoding()
String inline(String html)
Email addReplyTo(String email)
void embedRelative(String name, String path)
Email setFrom(String email)
void attach(String path, String description, String name)
final MailProvider provider
void attachExternal(URL url, String description, String name)
Email addCc(String email, String name)
void embed(String name, File file)
void setSentDate(Date date)
Email addReplyTo(String email, String name)
MailProvider getProvider()
Email addTo(String email)
Email addBcc(String email)
Email setFrom(String email, String name)
Email addTo(String email, String name)
void embedExternal(String name, String url)
void setMessage(String message)
static MailMessage of(MailProvider provider)
void setSubject(String subject)
void addHeader(String name, String value)
MailMessage(MailProvider provider)
Email addBcc(String email, String name)
void attachRelative(String path, String description, String name)
Email addCc(String email)
void setupMail(HtmlEmail email)