Skip to content

Commit a50bcc4

Browse files
committed
邮件测试成功 freemarker 加载ftl模板
1 parent cd658a3 commit a50bcc4

4 files changed

Lines changed: 140 additions & 3 deletions

File tree

src/main/java/com/rui/pro1/common/bean/EmailBean.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Map;
44

5+
import org.apache.commons.mail.EmailAttachment;
6+
57
/**
68
* 发送邮件bean
79
*
@@ -18,6 +20,9 @@ public class EmailBean {
1820
private Map<String, Object> map;// 模板邮件数据
1921
private String ftlName;
2022

23+
24+
private EmailAttachment emailAttachment;
25+
2126
public String getContext() {
2227
return context;
2328
}
@@ -66,4 +71,15 @@ public void setSubject(String subject) {
6671
this.subject = subject;
6772
}
6873

74+
public EmailAttachment getEmailAttachment() {
75+
return emailAttachment;
76+
}
77+
78+
public void setEmailAttachment(EmailAttachment emailAttachment) {
79+
this.emailAttachment = emailAttachment;
80+
}
81+
82+
83+
84+
6985
}

src/main/java/com/rui/pro1/common/utils/web/EmailUtil.java

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import org.apache.commons.lang3.StringUtils;
77
import org.apache.commons.mail.DefaultAuthenticator;
88
import org.apache.commons.mail.Email;
9+
import org.apache.commons.mail.EmailAttachment;
910
import org.apache.commons.mail.EmailException;
1011
import org.apache.commons.mail.HtmlEmail;
12+
import org.apache.commons.mail.MultiPartEmail;
1113
import org.apache.commons.mail.SimpleEmail;
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
@@ -114,6 +116,122 @@ public static void sendContextEmail(String context, String subject,
114116

115117
}
116118

119+
/**
120+
* 发送带符件邮件的简单文本邮件
121+
*
122+
* @param context
123+
* 邮件文本
124+
* @param subject
125+
* 主题
126+
* @param toEmail
127+
* 发送给谁的邮件地址
128+
* @param toEmailName
129+
* 名称
130+
* @throws EmailException
131+
*/
132+
public static void sendMultiPartEmail(String context, String subject,
133+
String toEmail, String toEmailName, EmailAttachment emailAttachment)
134+
throws EmailException {
135+
if (StringUtils.isBlank(context)) {
136+
throw new EmailException("邮件文本不能为空");
137+
}
138+
if (StringUtils.isBlank(toEmail)) {
139+
throw new EmailException("邮件接收人不能为空");
140+
}
141+
if (StringUtils.isBlank(subject)) {
142+
throw new EmailException("邮件主题不能为空");
143+
}
144+
if (emailAttachment != null) {
145+
throw new EmailException("附件不能为空");
146+
}
147+
148+
try {
149+
MultiPartEmail email = (MultiPartEmail) getDefaultEmailConfig(new MultiPartEmail());
150+
email.setSubject(subject);
151+
email.setMsg(toEmail);
152+
if (StringUtils.isBlank(toEmailName)) {
153+
// 接收人
154+
email.addTo(toEmail);
155+
} else {
156+
// 接收人
157+
email.addTo(toEmail, toEmailName);// 1067165280@qq.com
158+
// rui_dev@126.com
159+
}
160+
email.attach(emailAttachment);
161+
email.send();
162+
} catch (EmailException e) {
163+
logger.error("邮件发送异常!{}" + e.getMessage());
164+
e.printStackTrace();
165+
}
166+
167+
}
168+
169+
/**
170+
* 发送带符件邮件和ftl模板邮件
171+
*
172+
* @param map
173+
* ftl 对应数据
174+
* @param subject
175+
* 主题
176+
* @param toEmail
177+
* 发送给谁的邮件地址
178+
* @param toEmailName
179+
* 名称
180+
* @param EmailAttachment
181+
* 附件
182+
* @throws EmailException
183+
*/
184+
public static void sendMultiPartEmailAndFtl(Map<String, Object> map,
185+
String ftlName, String subject, String toEmail, String toEmailName,
186+
EmailAttachment emailAttachment) throws EmailException {
187+
if (StringUtils.isBlank(toEmail)) {
188+
throw new EmailException("邮件接收人不能为空");
189+
}
190+
if (StringUtils.isBlank(subject)) {
191+
throw new EmailException("邮件主题不能为空");
192+
}
193+
if (emailAttachment != null) {
194+
throw new EmailException("附件不能为空");
195+
}
196+
197+
try {
198+
FreeMarkerConfigurer configurer = FreeMarkerUtil
199+
.getFreeMarkerConfigurer();
200+
Template tpl = configurer.getConfiguration().getTemplate(ftlName);
201+
// if (map==null||map.size()<=0) {
202+
// }
203+
String ftlContext = FreeMarkerTemplateUtils
204+
.processTemplateIntoString(tpl, map);
205+
System.out.println(ftlContext);
206+
HtmlEmail email = (HtmlEmail) getDefaultEmailConfig(new HtmlEmail());
207+
// MimeMultipart multipart = new MimeMultipart("related");
208+
email.setHtmlMsg(ftlContext);
209+
// email.setTextMsg("xx");//文本消息的一部分。如果这将被用作替代文本 电子邮件客户端不支持HTML消息。
210+
email.setSubject(subject);
211+
// email.setMsg(toEmail);
212+
if (StringUtils.isBlank(toEmailName)) {
213+
// 接收人
214+
email.addTo(toEmail);
215+
} else {
216+
// 接收人
217+
email.addTo(toEmail, toEmailName);// 1067165280@qq.com
218+
// rui_dev@126.com
219+
}
220+
email.attach(emailAttachment);
221+
email.send();
222+
} catch (IOException e) {
223+
logger.error("freeMaraker模板加载异常!{}" + e.getMessage());
224+
e.printStackTrace();
225+
} catch (EmailException e) {
226+
logger.error("邮件发送异常!{}" + e.getMessage());
227+
e.printStackTrace();
228+
} catch (TemplateException e) {
229+
logger.error("processTemplateIntoString处理异常!{}" + e.getMessage());
230+
e.printStackTrace();
231+
}
232+
233+
}
234+
117235
/**
118236
* 发送简单文本邮件
119237
*
@@ -175,7 +293,7 @@ public static void sendFtlEmail(Map<String, Object> map, String ftlName,
175293
email.setHtmlMsg(ftlContext);
176294
// email.setTextMsg("xx");//文本消息的一部分。如果这将被用作替代文本 电子邮件客户端不支持HTML消息。
177295
email.setSubject(subject);
178-
//email.setMsg(toEmail);
296+
// email.setMsg(toEmail);
179297
if (StringUtils.isBlank(toEmailName)) {
180298
// 接收人
181299
email.addTo(toEmail);

src/main/resources/ftl/test.ftl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<html>
2-
2+
<head>
3+
<meta http-equiv="content-type" content="text/html;charset=utf8">
4+
</head>
35
<body>
4-
hi ftl模板测试邮件${hello}
6+
ftl 模板测试邮件${hello}
57
</body>
68
</html>

src/test/java/com/rui/pro1/email/SimpleEmailTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void attachmentTest() throws EmailException, MalformedURLException {
8686
// .setPath("C:/Users/Public/Pictures/Sample Pictures/befc0052e4021f50d6c8912214ae770f.jpg");
8787
attachment.setURL(new URL(
8888
"http://www.apache.org/images/asf_logo_wide.gif"));
89+
8990

9091
attachment.setDisposition(EmailAttachment.ATTACHMENT);
9192
attachment.setDescription("Picture of John");

0 commit comments

Comments
 (0)