JavaMail整合Spring实现邮件发送功能

简介
javaMail与spring整合完成后,可大大加大邮件发送效率。当服务器一启动,配置文件就已加载。直接保存用户信息时,邮件可直接发送,大大提高了效率。
1.引入坐标

javax.mailmail1.4.4org.springframeworkspring-context-support4.2.4.RELEASE

2.抽取MailUtils工具类
package cn.itcast.jk.utils; import java.util.Properties; import javax.mail.Address; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailUtils {/*** 发送邮件* @throws Exception * */public static void sendMsg(String toAddress,String subject,String content) throws Exception{//1.设置邮件的一些信息Properties props = new Properties(); //发送邮件的服务器地址props.put("mail.smtp.host", "smtp.163.com"); //smtp.qq.com stmp.sina.comprops.put("mail.smtp.auth", "true"); //2.创建Session对象Session session =Session.getInstance(props); //3.创建出MimeMessage,邮件的消息对象MimeMessage message = new MimeMessage(session); //4.设置发件人Address fromAddr = new InternetAddress("发件人邮箱"); message.setFrom(fromAddr); //5.设置收件人Address toAddr=new InternetAddress(toAddress); message.setRecipient(RecipientType.TO, toAddr); //6.设置邮件的主题message.setSubject(subject); //7.设置邮件的正文message.setText(content); message.saveChanges(); //保存更新//8.得到火箭Transport transport = session.getTransport("smtp"); transport.connect("smtp.163.com","发件人邮箱","发件人密码"); //设置了火箭的发射地址transport.sendMessage(message, message.getAllRecipients()); //发送具体内容及接收人transport.close(); }}

3.mailproperties.xml
mail.host=smtp.163.commail.username=你的邮箱名——也就是@前面的东西mail.password=密码mail.from=你的邮箱地址

4.创建applicationContext-mail.xml
JavaMail的配置文件【JavaMail整合Spring实现邮件发送功能】truetrue0

5.applicationContext.xml引入applicationContext-mail.xml

6.applicationContext-service.xml中注值

mailMessage与mailSender注入到UserServiceImpl中
7.UserServiceImpl类中的saveorUpdate方法中加入
//spring整合javaMail需要注入:private SimpleMailMessage mailMessage; private JavaMailSender mailSender; public void setMailMessage(SimpleMailMessage mailMessage) {this.mailMessage = mailMessage; }public void setMailSender(JavaMailSender mailSender) {this.mailSender = mailSender; }public void saveOrUpdate(final User entity) {if(UtilFuns.isEmpty(entity.getId())){//判断id是否有值//说明id没有值,说明保存entity.setState(1); //1代表可用String id = UUID.randomUUID().toString(); entity.setId(id); entity.getUserinfo().setId(id); //设置初始密码 需要将默认的密码加密后保存到数据库entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName())); //final就是延长对象的生命周期,不然entity只能在saveOrUpdate中使用,使用完成后方法弹栈,而run方法内就无法再使用之前定义好的entity。//使用spring与javaMail实现新员工入职时邮件的发送//使用线程并try-catch的目的就是如果邮件发送失败,也不影响信息保存到数据库。邮件发送成为了一个独立的过程。Thread th = new Thread(new Runnable(){public void run(){try {mailMessage.setTo(entity.getUserinfo().getEmail()); mailMessage.setSubject("新员工入职信息"); mailMessage.setText("欢迎"+entity.getUserinfo().getName()+"加入廊坊思创志远科技有限公司,您在公司的账号:"+entity.getUserName()+",密码:"+SysConstant.DEFAULT_PASS); mailSender.send(mailMessage); } catch (MailException e) {e.printStackTrace(); }}}); th.start(); }baseDao.saveOrUpdate(entity); }

JavaMail的发送机制
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读