欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaMail整合Spring實現(xiàn)郵件發(fā)送功能

 更新時間:2022年08月11日 10:05:41   作者:Mr小林  
這篇文章主要為大家詳細介紹了JavaMail整合Spring實現(xiàn)郵件發(fā)送功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡介

javaMail與spring整合完成后,可大大加大郵件發(fā)送效率。當服務器一啟動,配置文件就已加載。直接保存用戶信息時,郵件可直接發(fā)送,大大提高了效率。

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 {
? ? /**
? ? ?* 發(fā)送郵件
? ? ?* @throws Exception?
? ? ?*?
? ? ?*/
? ? public static void sendMsg(String toAddress,String subject,String content) throws Exception{
? ? ? ? //1.設置郵件的一些信息
? ? ? ? Properties props = new Properties();
? ? ? ? //發(fā)送郵件的服務器地址
? ? ? ? props.put("mail.smtp.host", "smtp.163.com");//smtp.qq.com stmp.sina.com
? ? ? ? props.put("mail.smtp.auth", "true");
? ? ? ? //2.創(chuàng)建Session對象
? ? ? ? Session session =Session.getInstance(props);
? ? ? ? //3.創(chuàng)建出MimeMessage,郵件的消息對象
? ? ? ? MimeMessage message = new MimeMessage(session);
? ? ? ? //4.設置發(fā)件人
? ? ? ? Address fromAddr = new InternetAddress("發(fā)件人郵箱");
? ? ? ? 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","發(fā)件人郵箱","發(fā)件人密碼"); //設置了火箭的發(fā)射地址

? ? ? ? transport.sendMessage(message, message.getAllRecipients());//發(fā)送具體內(nèi)容及接收人

? ? ? ? transport.close();
? ? }
}

3.mailproperties.xml

mail.host=smtp.163.com
mail.username=你的郵箱名——也就是@前面的東西
mail.password=密碼
mail.from=你的郵箱地址

4.創(chuàng)建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"/>


? ? <!-- 簡單消息對象創(chuàng)建 -->
? ? <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
? ? ? ? ?<property name="from" value="${mail.from}"></property>
? ? </bean>

? ? <!-- 2.創(chuàng)建發(fā)送器 --> ? ?
? ? <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);

? ? ? ? ? ? //設置初始密碼 需要將默認的密碼加密后保存到數(shù)據(jù)庫
? ? ? ? ? ? entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName()));
//final就是延長對象的生命周期,不然entity只能在saveOrUpdate中使用,使用完成后方法彈棧,而run方法內(nèi)就無法再使用之前定義好的entity。
//使用spring與javaMail實現(xiàn)新員工入職時郵件的發(fā)送
//使用線程并try-catch的目的就是如果郵件發(fā)送失敗,也不影響信息保存到數(shù)據(jù)庫。郵件發(fā)送成為了一個獨立的過程。
? ? ? ? ? ? Thread th = new Thread(new Runnable(){
? ? ? ? ? ? ? ? public void run(){
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? mailMessage.setTo(entity.getUserinfo().getEmail());
? ? ? ? ? ? ? ? ? ? ? ? mailMessage.setSubject("新員工入職信息");
? ? ? ? ? ? ? ? ? ? ? ? mailMessage.setText("歡迎"+entity.getUserinfo().getName()+"加入廊坊思創(chuàng)志遠科技有限公司,您在公司的賬號:"+entity.getUserName()+",密碼:"+SysConstant.DEFAULT_PASS);
? ? ? ? ? ? ? ? ? ? ? ? mailSender.send(mailMessage);
? ? ? ? ? ? ? ? ? ? } catch (MailException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? th.start();
? ? ? ? }
? ? ? ? baseDao.saveOrUpdate(entity);
}?

JavaMail的發(fā)送機制 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Mybatis中傳遞多個參數(shù)的4種方法總結

    Mybatis中傳遞多個參數(shù)的4種方法總結

    這篇文章主要給大家介紹了關于Mybatis中傳遞多個參數(shù)的4種方法,并且介紹了關于使用Mapper接口時參數(shù)傳遞方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2018-04-04
  • 如何在springBoot下搭建日志框架

    如何在springBoot下搭建日志框架

    這篇文章主要介紹了如何在springBoot下搭建日志框架,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • 淺談Zookeeper開源客戶端框架Curator

    淺談Zookeeper開源客戶端框架Curator

    這篇文章主要介紹了淺談Zookeeper開源客戶端框架Curator的相關內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • Java 中分形圖的幾種方法詳解

    Java 中分形圖的幾種方法詳解

    這篇文章主要介紹了Java 中幾種分形的方法詳解的相關資料,需要的朋友可以參考下
    2017-07-07
  • spring基于通用Dao的多數(shù)據(jù)源配置詳解

    spring基于通用Dao的多數(shù)據(jù)源配置詳解

    這篇文章主要為大家詳細介紹了spring基于通用Dao的多數(shù)據(jù)源配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下解
    2018-03-03
  • springboot項目中PropertySource如何讀取yaml配置文件

    springboot項目中PropertySource如何讀取yaml配置文件

    這篇文章主要介紹了springboot項目中PropertySource如何讀取yaml配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • mybatis-plus自動裝配時間失效的解決

    mybatis-plus自動裝配時間失效的解決

    本文主要介紹了mybatis-plus自動裝配時間失效,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • SpringBoot中使用MongoDB的連接池配置

    SpringBoot中使用MongoDB的連接池配置

    由于MongoDB的客戶端本身就是一個連接池,因此,我們只需要配置客戶端即可,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Spring Bean如何實現(xiàn)自動配置代碼實例

    Spring Bean如何實現(xiàn)自動配置代碼實例

    這篇文章主要介紹了Spring Bean如何實現(xiàn)自動配置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Java實現(xiàn)的圖片高質量縮放類定義與用法示例

    Java實現(xiàn)的圖片高質量縮放類定義與用法示例

    這篇文章主要介紹了Java實現(xiàn)的圖片高質量縮放類定義與用法,涉及java針對圖片的運算與轉換等相關操作技巧,需要的朋友可以參考下
    2017-11-11

最新評論