一、Spring Mail API
Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送、添加附件。
1、邮件发送的核心接口:MailSender,实现类JavaMailSenderImpl;
2、邮件对象:SimpleMailMessage 封装了简单邮件的属性如from, to,cc, subject,text;
org.springframework.mail.javamail.JavaMailSender和org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。
引入相关jar包:spring 相关jar包 mail.jar 和 activation.jar
二、Spring mail 发送邮件
1、配置
(1)mail.properties
# mail host
mail.host=smtp.163.com
# smtp port
mail.port=25
mail.encoding=UTF-8
mail.smtp.auth=true
# request timeout
mail.smtp.timeout=25000
mail.username=cac2020@163.com
mail.password=123456
mail.from=email@163.com;
(2)spring-mail.xml
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
${mail.host}
${mail.username}
${mail.password}
${mail.port}
${mail.encoding}
smtp
${mail.smtp.auth}
false
${mail.smtp.timeout}
true
(3)发送邮件
packagecom.jeecg.mail.controller;importjavax.mail.MessagingException;importjavax.servlet.http.HttpServletRequest;importorg.jeecgframework.core.common.controller.BaseController;importorg.jeecgframework.core.common.model.json.AjaxJson;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.servlet.ModelAndView;importcom.jeecg.mail.service.MailServiceImpl;
@Scope("prototype")
@Controller
@RequestMapping("/MailController")public class MailController extendsBaseController
{
@AutowiredprivateMailServiceImpl mailService;
@RequestMapping(params= { "mail", "page"})public ModelAndView mail(@RequestParam("page") String page,HttpServletRequest request)
{
ModelAndView modelAndView= newModelAndView(page);returnmodelAndView;
}
@RequestMapping(params= "sendTextMail", method ={ RequestMethod.POST,RequestMethod.GET})
@ResponseBodypublicAjaxJson sendTextMail()
{
AjaxJson json= newAjaxJson();try{
mailService.sendTextMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个纯文本邮件","哈哈哈哈哈哈 这是一个纯文本邮件");
}catch(Exception e)
{
json.setMsg(e.getMessage());
e.printStackTrace();
}returnjson;
}
@RequestMapping(params= "sendHTMLMail", method ={ RequestMethod.POST,RequestMethod.GET})
@ResponseBodypublicAjaxJson sendHTMLMail()
{
AjaxJson json= newAjaxJson();try{
mailService.sendHTMLMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个HTML邮件","
hello!!spring html Mail
");}catch(MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}returnjson;
}
@RequestMapping(params= "sendImageMail", method ={ RequestMethod.POST,RequestMethod.GET})
@ResponseBodypublicAjaxJson sendImageMail(HttpServletRequest request)
{
AjaxJson json= newAjaxJson();try{
String jpgpath= request.getSession().getServletContext().getRealPath("/images/1.jpg");
mailService.sendImageMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个含图片邮件","
hello!!spring image html mail
","1",jpgpath);
}catch(MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}returnjson;
}
@RequestMapping(params= "sendAttachMail", method ={ RequestMethod.POST,RequestMethod.GET})
@ResponseBodypublicAjaxJson sendAttachMail(HttpServletRequest request)
{
AjaxJson json= newAjaxJson();try{
String filepath= request.getSession().getServletContext().getRealPath("/images/测试.rar");
mailService.sendAttachMail("cac2020@163.com", "530589482@qq.com", "cac2020@126.com", "【测试】发送一个含附件邮件","","测试.rar",
filepath);
}catch(MessagingException e)
{
e.printStackTrace();
json.setMsg(e.getMessage());
}returnjson;
}
}
注意:
1、Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或),其余的会被Spring忽略掉
Spring 配置文件中标签如果有多个properties文件 location='classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties'
参考:
http://blog.csdn.net/smcwwh/article/details/7095027
http://blog.csdn.net/zheng0518/article/details/50179213
http://www.oschina.net/code/snippet_2416648_55116
http://blog.csdn.net/zh921112/article/details/38397801
http://blog.csdn.net/houxuehan/article/details/52184736
http://www.zgxue.com/167/1677686.html