JavaMail与Spring整合过程解析
作者:Mr小林 发布时间:2022-07-06 23:53:54
标签:java,mail,spring,整合
这篇文章主要介绍了JavaMail与Spring整合过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
简介
javaMail与spring整合完成后,可大大加大邮件发送效率。当服务器一启动,配置文件就已加载。直接保存用户信息时,邮件可直接发送,大大提高了效率。
1.引入坐标
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
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.com
props.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.com
mail.username=你的邮箱名——也就是@前面的东西
mail.password=密码
mail.from=你的邮箱地址
4.创建applicationContext-mail.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<description>JavaMail的配置文件</description>
<!-- 加载mail.properties配置文件 -->
<context:property-placeholder location="classpath:mail.properties"/>
<!-- 简单消息对象创建 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 2.创建发送器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
5.applicationContext.xml引入applicationContext-mail.xml
<import resource="classpath:spring/applicationContext-mail.xml"/>
6.applicationContext-service.xml中注值
<bean id="userService" class="cn.itcast.jk.service.impl.UserServiceImpl">
<property name="baseDao" ref="baseDao"></property>
<property name="mailMessage" ref="mailMessage"></property>
<property name="mailSender" ref="mailSender"></property>
</bean>
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);
}
来源:https://blog.csdn.net/mrlin6688/article/details/53496479


猜你喜欢
- 1、概念:MyBatis中的延迟加载,也称为懒加载,是指在进行表的关联查询时,按照设置延迟规则推迟对关联对象的select查询。例如在进行一
- 本文实例讲述了C#编程调用Cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下:using System;using Sys
- 目录一、什么是Spring二、什么是IOC三、快速搭建框架环境四、spring之依赖注入五、详解Spring框架的IOC之注解方式七、Spr
- 需要设置允许不安全代码.....项目->属性->生成->允许不安全代码/// <summary> &
- 本文实例讲述了java读取properties文件的方法。分享给大家供大家参考。具体实现方法如下:package com.test.demo
- 一、概述xml整合第三方框架有两种整合方案:不需要自定义名空间,不需要使用Spring的配置文件配置第三方框架本身内容,例如:MyBatis
- 使用applicationContext.xml配置文件SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提
- 一、题干输入一个9*9二维数组表示数独,已经填入的数字用1-9表示,待填入的数字用0表示,试写一个算法解出数独并输出。二、思路容易想到回溯法
- 简介FTP是TCP/IP协议组中的协议之一,包括两个组成部分,一是FTP服务端,二是FTP客户端,其中FTP服务器用来存储文件,用户可以使用
- 本文实例为大家分享了Java Web实现简易图书管理系统的具体代码,供大家参考,具体内容如下前言首先实现的是用户的登录注册,注册成功后自动跳
- 场景:使用MyBatis批量查询(select)、批量插入(insert)、批量更新(update)、批量删除(delete)操作MySQL
- Android Studio连接手机设备教程,供大家参考,具体内容如下一、ADB环境配置1.查看自己Android Studio配置的sdk
- 由于spring和es的集成并不是特别友好,es的高低版本兼容问题、api更新频率高等问题,所以我选择是官网提供的原生Client(Rest
- 本节只是介绍实战部分,具体的理论参数,请自行百度。所需工具:linux服务器 Jmeter测试工具 xshell &
- 前言Mybatis真正强大的地方在于SQL映射语句,这也是它的魅力所在。相对于它强大的功能,SQL映射文件的配置却非常简单,我上篇文章语句讲
- 在学习安卓的最初过程中我们学的都是最基本的一个活动,只有一个活动的应用也太简单了吧,没错我们的最求应该更高点,不管你创建多少个活动,接下里我
- 应用场景:在Android开发过程中,有时需要调用手机自身设备的功能,本文侧重摄像头拍照功能的调用。知识点介绍:使用权限:调用手机自身设备功
- 本文实例为大家分享了java实现录音播放的具体代码,供大家参考,具体内容如下需求:1.实现可以从麦克风进行录音2.可以停止录音3.实现播放录
- 一、前言二、案例需求1.编写login.html登录页面,username&password两个输入框2.使用Druid数据库连接池
- 这篇文章主要介绍了Java中的3种输入方式实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可