最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaMail實(shí)現(xiàn)郵件發(fā)送機(jī)制

 更新時(shí)間:2022年08月11日 10:04:21   作者:Mr小林  
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)郵件發(fā)送機(jī)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

概念

JavaMail,顧名思義,提供給開發(fā)者處理電子郵件相關(guān)的編程接口。它是Sun發(fā)布的用來(lái)處理email的API。它可以方便地執(zhí)行一些常用的郵件傳輸。我們可以基于JavaMail開發(fā)出類似于Microsoft Outlook的應(yīng)用程序。

應(yīng)用場(chǎng)景

一般在系統(tǒng)的注冊(cè)模塊,當(dāng)用戶填入注冊(cè)信息的郵箱時(shí),點(diǎn)擊保存。系統(tǒng)根據(jù)用戶的信息會(huì)自動(dòng)給用戶發(fā)送一封郵件,上面有用戶的基本信息和注意事項(xiàng),也可以用此方法實(shí)現(xiàn)用戶的激活。

代碼實(shí)現(xiàn)

普通方式一

1.首先引入javaMail的mail坐標(biāo)即jar包

jar包:mail:1.4.1

坐標(biāo):

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>

2.測(cè)試代碼實(shí)現(xiàn)

首先引入junit 測(cè)試包

package mail.test;
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;

import org.junit.Test;

public class Mail01Test {

? ? @Test
? ? public void testJavaMail() throws Exception{
? ? ? ? //1.設(shè)置郵件的一些信息
? ? ? ? Properties props = new Properties();
? ? ? ? //發(fā)送郵件的服務(wù)器地址
? ? ? ? props.put("mail.smtp.host", "smtp.163.com");// ?stmp.qq.com ? smtp.sina.com
? ? ? ? props.put("mail.smtp.auth", "true");

? ? ? ? //2.創(chuàng)建Session對(duì)象
? ? ? ? Session session =Session.getInstance(props);

? ? ? ? //3.創(chuàng)建出MimeMessage,郵件的消息對(duì)象
? ? ? ? MimeMessage message = new MimeMessage(session);

? ? ? ? //4.設(shè)置發(fā)件人
? ? ? ? Address fromAddr = new InternetAddress("發(fā)件人郵箱");
? ? ? ? message.setFrom(fromAddr);

? ? ? ? //5.設(shè)置收件人
? ? ? ? Address toAddr = new InternetAddress("收件人郵箱");
? ? ? ? message.setRecipient(RecipientType.TO, toAddr);

? ? ? ? //6.設(shè)置郵件的主題
? ? ? ? message.setSubject("項(xiàng)目進(jìn)展順序");

? ? ? ? //7.設(shè)置郵件的正文
? ? ? ? message.setText("項(xiàng)目進(jìn)展順序,所有兄弟們都非常努力,老板今天可以請(qǐng)吃飯");
? ? ? ? message.saveChanges();//保存更新

? ? ? ? //8.得到火箭
? ? ? ? Transport transport = session.getTransport("smtp");
? ? ? ? transport.connect("smtp.163.com", "發(fā)件人郵箱", "發(fā)件人密碼");//設(shè)置了火箭的發(fā)射地址
? ? ? ? transport.sendMessage(message, message.getAllRecipients());//發(fā)送具體內(nèi)容及接收人 ? ??
? ? ? ? transport.close();
? ? }

}

普通方式二

方式二可以帶附件和圖片

1.測(cè)試代碼:

package mail.test;
import java.io.File;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Mail03Test {

? ? @Test
? ? public void testJavaMail() throws Exception{
? ? ? ? ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-mail.xml");

? ? ? ? //得到發(fā)送器
? ? ? ? JavaMailSender mailSender = (JavaMailSender) ac.getBean("mailSender");

? ? ? ? //得到一個(gè)MimeMessage對(duì)象
? ? ? ? MimeMessage message = mailSender.createMimeMessage();

? ? ? ? //產(chǎn)生出一個(gè)MimeMessageHelper helper?
? ? ? ? MimeMessageHelper helper = new MimeMessageHelper(message, true);//工具類本質(zhì)是操作message消息 ? true代表可以帶附件,圖片

? ? ? ? //3.使用helper工具類,設(shè)置郵件的發(fā)送者,接收者,主題,正文
? ? ? ? helper.setFrom("發(fā)送郵箱");
? ? ? ? helper.setTo("接收郵箱");
? ? ? ? helper.setSubject("發(fā)送圖片和附件");
? ? ? ? helper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1><a href=http://www.baidu.com>baidu</a><img src='cid:image' /></body></html>

//指定cid的取值
? ? ? ? FileSystemResource imgResource = new FileSystemResource(new File("E:/01分配權(quán)限原理分析.png"));
? ? ? ? helper.addInline("image", imgResource);

? ? ? ? //帶附件
? ? ? ? FileSystemResource fileResource = new FileSystemResource(new File("E:/javamail1_4_4.zip"));
? ? ? ? helper.addAttachment("javamail1_4_4.zip", fileResource);

? ? ? ? //發(fā)送
? ? ? ? mailSender.send(message);

? ? }
}

2.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"/>


? ? <!-- 簡(jiǎn)單消息對(duì)象創(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>

2.mail.properties文件:

mail.host=smtp.163.com
mail.username=@前面的用戶名
mail.password=密碼
mail.from=發(fā)件人郵箱全拼

JavaMail與Spring整合點(diǎn)擊即可進(jìn)入

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能全過(guò)程

    Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能全過(guò)程

    在Java開發(fā)過(guò)程中實(shí)現(xiàn)用戶的注冊(cè)功能是最基本的,這篇文章主要給大家介紹了關(guān)于Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析

    JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析

    這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java中CaffeineCache自定義緩存時(shí)間的實(shí)現(xiàn)

    Java中CaffeineCache自定義緩存時(shí)間的實(shí)現(xiàn)

    本文主要介紹了Java中CaffeineCache自定義緩存時(shí)間的實(shí)現(xiàn),通過(guò)聲明緩存value值holder對(duì)象并創(chuàng)建緩存容器,可以為不同的key值指定不同的過(guò)期時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-02-02
  • Java基于高精度整型實(shí)現(xiàn)fibonacci數(shù)列的方法

    Java基于高精度整型實(shí)現(xiàn)fibonacci數(shù)列的方法

    這篇文章主要介紹了Java基于高精度整型實(shí)現(xiàn)fibonacci數(shù)列的方法,是比較典型的算法,需要的朋友可以參考下
    2014-09-09
  • Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    Spring?boot2.0?實(shí)現(xiàn)日志集成的方法(2)

    這篇文章主要介紹了Spring?boot2.0?實(shí)現(xiàn)日志集成的方法,上一章講解了spring?boot日志簡(jiǎn)單集成,這篇我們將日志進(jìn)行分類,常規(guī)日志、異常日志、監(jiān)控日志等,需要將日志輸出到不同的文件,具體內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Java解決LocalDateTime傳輸前端為時(shí)間的數(shù)組

    Java解決LocalDateTime傳輸前端為時(shí)間的數(shù)組

    本文主要介紹了Java解決LocalDateTime傳輸前端為時(shí)間的數(shù)組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • 必須要學(xué)會(huì)的JMM與volatile

    必須要學(xué)會(huì)的JMM與volatile

    這篇文章主要介紹了必須要學(xué)會(huì)的JMM與volatile,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • @Autowired 自動(dòng)注入接口失敗的原因及解決

    @Autowired 自動(dòng)注入接口失敗的原因及解決

    這篇文章主要介紹了@Autowired 自動(dòng)注入接口失敗的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Linux(centos7)安裝jdk1.8的詳細(xì)步驟

    Linux(centos7)安裝jdk1.8的詳細(xì)步驟

    Linux的使用相信大家都要用到j(luò)ava吧,在使用java前我們得先安裝jdk以及配置環(huán)境變量等工作,下面這篇文章主要給大家介紹了關(guān)于Linux(centos7)安裝jdk1.8的詳細(xì)步驟,需要的朋友可以參考下
    2023-10-10
  • RocketMQ消息發(fā)送流程源碼剖析

    RocketMQ消息發(fā)送流程源碼剖析

    這篇文章主要為大家介紹了RocketMQ消息發(fā)送流程源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08

最新評(píng)論

额尔古纳市| 黄大仙区| 油尖旺区| 龙陵县| 上虞市| 织金县| 双牌县| 织金县| 原阳县| 织金县| 上虞市| 中卫市| 册亨县| 都匀市| 固安县| 麻江县| 三明市| 中西区| 舞钢市| 尚义县| 江油市| 永年县| 新安县| 错那县| 红桥区| 怀集县| 文山县| 金昌市| 鄂温| 工布江达县| 大名县| 兴国县| 东宁县| 余干县| 临湘市| 柳州市| 合阳县| 革吉县| 紫金县| 嘉祥县| 凤凰县|