java邮件服务系统开发的_Java邮件服务学习之四:邮箱服务客户端Spring Mail

一、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

%5C%22cid:1%5C%22","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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值