pring对邮件发送支持的很好,我们只要配置好邮件发送器,写好邮件发送具体操作类的方法,那么实现就不是很难的事,不过这个过程中经常会出现这样或那样的错误,所以待讲完本篇后我会总体总结下spring邮件发送容易发生的错误及解决办法
关于邮件发送,不外乎有这么几种情况,一种是纯文本的发送,一种是HTML形式的发送,一种就是发送时带有附件,一种就是发送时采取的事群发,针对这几个我将一一来讲下,我的代码中将第一中归为一大类,后面三种归为一代类,所以将会出现两种配置文件
第一大类邮件发送【纯文本发送】
必须包:spring.jar,common-logging.jar,mail.jar,servlet.jar,common-collection.jar
首先我们来看spring配置文件applicationContext.xml
- <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
- <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="smtp.163.com" />
- <property name="port" value="25" />
- <property name="username" value="emailusername" />
- <property name="password" value="emailpassword" />
- <property name="defaultEncoding" value="utf-8" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
-
- <bean id="emailService" class="com.javacrazyer.comon.SendOrderConfirmationEmailAdvice">
- <property name="mailSender" ref="mailSender" />
- </bean>
-
- </beans></span>
用到的实体类Order.java
- <span style="font-size: medium;">package com.javacrazyer.comon;
-
- import java.io.Serializable;
-
- public class Order implements Serializable {
-
-
-
- private int orderId;
- private String username;
- private String useremail;
-
- public int getOrderId() {
- return orderId;
- }
-
- public void setOrderId(int orderId) {
- this.orderId = orderId;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getUseremail() {
- return useremail;
- }
-
- public void setUseremail(String useremail) {
- this.useremail = useremail;
- }
-
- }
- </span>
邮件发送类的接口类MailService.java
- <span style="font-size: medium;">package com.javacrazyer.comon;
-
-
-
-
-
-
-
- public interface MailService {
-
-
- void sendOrderMail(Order order);
-
- }
- </span>
具体实现的邮件发送类SendOrderConfirmationEmailAdvice.java
- <span style="font-size: medium;">package com.javacrazyer.comon;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
-
- import org.springframework.mail.MailSender;
- import org.springframework.mail.SimpleMailMessage;
-
- public class SendOrderConfirmationEmailAdvice implements MailService{
-
- private String from="emailusername@163.com";
- private String registeTemplateName = "com/javacrazyer/comon/mail_registe.txt";
- private MailSender mailSender;
-
- public void setMailSender(MailSender mailSender) {
- this.mailSender = mailSender;
- }
-
-
- public void sendOrderMail(Order order) {
-
- SimpleMailMessage mail = new SimpleMailMessage();
- mail.setFrom(this.from);
- mail.setTo(order.getUseremail());
- mail.setSubject("恭喜你成功注册成为SOMEDAY商城的会员!");
- mail.setText(loadTemplateContent(registeTemplateName).replaceAll("\\$\\{LOGINNAME\\}", order.getUsername()));
- this.mailSender.send(mail);
-
- }
-
-
-
- private String loadTemplateContent(String templateName){
- StringBuilder sb = new StringBuilder();
- BufferedReader br= null;
- try{
- br = new BufferedReader(
- new InputStreamReader(
- Thread.currentThread()
- .getContextClassLoader()
- .getResourceAsStream(templateName), "UTF-8"));
-
- String lineSeparator = System.getProperty("line.separator");
- for(String str = null; (str = br.readLine()) != null;){
- sb.append(str);
- sb.append(lineSeparator);
- }
- }catch(IOException e){
- e.printStackTrace();
- }finally{
- if(br != null){
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- return sb.toString();
- }
-
-
-
- }</span>
用到的mail_registe.txt
- <span style="font-size: medium;">亲爱的${LOGINNAME}:您好!
- 恭喜你成为SOMEDAY商城的会员!
-
- 你的登录用户名为:${LOGINNAME}
- 你的登录口令为:******(隐藏)
-
- 本站网址:http:
- 联系邮箱:hr@yoursite.cn</span>
测试发送
- <span style="font-size: medium;">package com.javacrazyer.service.test;
-
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.javacrazyer.comon.MailService;
- import com.javacrazyer.comon.Order;
-
- public class UserServiceTest {
-
- @Test
- public void test() throws InterruptedException {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- MailService sf = (MailService) context.getBean("emailService");
- Order order = new Order();
- order.setUsername("cheneywu");
- order.setUseremail("xxxxxx@qq.com");
- sf.sendOrderMail(order);
- }
- }</span>
注意查收邮箱中的信息哦
第二大类邮件发送【HTML格式的发送,群发,附件】
除了上面包外,还需要spring-webmvc.jar,freemarker.jar,为什么需要freemarker呢,因为要发送HTML格式的文件,所以要先写好HTML内容的文件,然后用模板freemarker匹配其中的值,Spring中对freemarker也有很好的支持
spring配置文件applicatonContext-html.xml
- <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <bean id="freeMarker"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath" value="classpath:mailTemplate" />
-
- <property name="freemarkerSettings">
- <props>
- <prop key="template_update_delay">1800</prop>
- <prop key="default_encoding">UTF-8</prop>
- <prop key="locale">zh_CN</prop>
- </props>
- </property>
- </bean>
-
-
- <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="smtp.163.com" />
- <property name="port" value="25" />
- <property name="username" value="emailusername" />
- <property name="password" value="emailpassword" />
- <property name="defaultEncoding" value="utf-8" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
- <bean id="emailService" class="com.javacrazyer.comon.EmailService">
- <property name="mailSender" ref="mailSender"></property>
- <property name="freeMarkerConfigurer" ref="freeMarker"></property>
- </bean>
- </beans> </span>
模板文件目mailTemplate下的模板文件registe.ftl 【这个文件无名字、后缀无所谓,只要内容是HTML内容即可】
- <span style="font-size: medium;"><html>
- <head>
- <meta http-equiv="content-type" content="text/html;charset=utf8">
- </head>
- <body>
- 恭喜您成功注册!您的用户名为:<font color=‘red‘ size=‘30‘>${LOGINNAME}</font>
- </body>
- </html> </span>
具体邮件发送类EmailService.java
- <span style="font-size: medium;">package com.javacrazyer.comon;
-
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeUtility;
-
- import org.springframework.mail.MailSender;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
- import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
-
- import freemarker.template.Template;
-
- public class EmailService implements MailService {
-
- private JavaMailSender mailSender;
- private FreeMarkerConfigurer freeMarkerConfigurer = null;
-
- public void setMailSender(JavaMailSender mailSender) {
- this.mailSender = mailSender;
- }
-
-
-
- public void setFreeMarkerConfigurer(
- FreeMarkerConfigurer freeMarkerConfigurer) {
- this.freeMarkerConfigurer = freeMarkerConfigurer;
- }
-
-
-
- public void sendOrderMail(Order order) {
- MimeMessage msg = mailSender.createMimeMessage();
-
-
- try {
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
- helper.setTo(order.getUseremail());
- helper.setFrom("emailusername@163.com");
- helper.setSubject("你好,恭喜你注册成功");
- String htmlText;
- htmlText = getMailText(order.getUsername());
- helper.setText(htmlText, true);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- mailSender.send(msg);
- }
-
-
-
-
- public void sendBatchEmail(Order order, List<String> address) {
-
- MimeMessage msg = mailSender.createMimeMessage();
-
- try {
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
- String toList = getMailList(address);
- InternetAddress[] iaToList = new InternetAddress().parse(toList);
- msg.setRecipients(Message.RecipientType.TO, iaToList);
- helper.setFrom("emailusername@163.com");
- helper.setSubject("你好,恭喜你注册成功");
- String htmlText = getMailText(order.getUsername());
- helper.setText(htmlText, true);
-
- helper.addInline("a", new File("E:/11.jpg"));
- helper.addInline("b", new File("E:/12.jpg"));
- File file = new File("E:/各种框架图介绍.docx");
-
- helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
- } catch (MessagingException e) {
-
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
-
- e.printStackTrace();
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
-
-
-
-
-
- mailSender.send(msg);
- }
-
-
-
-
- public String getMailList(List<String> to) {
- StringBuffer toList = new StringBuffer();
- int length = to.size();
- if (to != null && length < 2) {
- toList.append(to.get(0));
- } else {
- for (int i = 0; i < length; i++) {
- toList.append(to.get(i));
- if (i != (length - 1)) {
- toList.append(",");
- }
- }
- }
- return toList.toString();
- }
-
-
- private String getMailText(String content) throws Exception {
- String htmlText = "";
-
- Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(
- "registe.ftl");
- Map map = new HashMap();
- map.put("LOGINNAME", content);
-
- htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);
- return htmlText;
- }
-
- }</span>
单一发送HTML格式的邮件测试
- <span style="font-size: medium;">package com.javacrazyer.service.test;
-
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.javacrazyer.comon.MailService;
- import com.javacrazyer.comon.Order;
-
- public class UserServiceTest {
-
- @Test
- public void test() throws InterruptedException {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext-html.xml");
- MailService sf = (MailService) context.getBean("emailService");
- Order order = new Order();
- order.setUsername("cheneywu");
- order.setUseremail("xxxxxx@qq.com");
- sf.sendOrderMail(order);
- }
- }
- </span>
收到邮件了
群发,带附件的测试
- <span style="font-size: medium;">package com.javacrazyer.service.test;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.javacrazyer.comon.EmailService;
- import com.javacrazyer.comon.Order;
-
- public class UserServiceTest {
-
- @Test
- public void test() throws InterruptedException {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext-html.xml");
- EmailService sf = (EmailService) context.getBean("emailService");
- Order order = new Order();
- order.setUsername("cheneywu");
- List<String> maillist = new ArrayList<String>();
- maillist.add("XXXXXX@qq.com");
- maillist.add("YYYYYY@qq.com");
- sf.sendBatchEmail(order, maillist);
- }
- }
- </span>
收到邮件了哦
看到了没有,这个上边有两个接收人,下边的是附件