JavaMail實(shí)現(xiàn)郵件發(fā)送機(jī)制
概念
JavaMail,顧名思義,提供給開發(fā)者處理電子郵件相關(guān)的編程接口。它是Sun發(fā)布的用來處理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í)有所幫助,也希望大家多多支持腳本之家。
- JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能
- JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
- 基于JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
- 基于JavaMail的Java郵件發(fā)送
- JavaMail郵件發(fā)送機(jī)制詳解
- 基于JavaMail實(shí)現(xiàn)郵件發(fā)送
- 基于JavaMail的Java實(shí)現(xiàn)復(fù)雜郵件發(fā)送功能
- 基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能
- JavaMail實(shí)現(xiàn)郵件發(fā)送的方法
- JavaMail實(shí)現(xiàn)帶附件的郵件發(fā)送
相關(guān)文章
java使用RSA加密方式實(shí)現(xiàn)數(shù)據(jù)加密解密的代碼
這篇文章給大家分享java使用RSA加密方式實(shí)現(xiàn)數(shù)據(jù)加密解密,通過實(shí)例代碼文字相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下2019-11-11
Java 使用getClass().getResourceAsStream()方法獲取資源
這篇文章主要介紹了Java 使用getClass().getResourceAsStream()方法獲取資源的相關(guān)資料,這里主要講解哪種方式可以獲取到文件資源,需要的朋友可以參考下2017-07-07
關(guān)于遠(yuǎn)程調(diào)用RestTemplate的使用避坑指南
這篇文章主要介紹了關(guān)于遠(yuǎn)程調(diào)用RestTemplate的使用避坑指南,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊功能,文中示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以參考一下2022-05-05
Java實(shí)現(xiàn)簡(jiǎn)單的遞歸操作方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)簡(jiǎn)單的遞歸操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java語言Iterator轉(zhuǎn)換成 List的方法
在 Java 中,迭代器(Iterator)是一種用于遍歷集合中元素的對(duì)象,它提供了一種簡(jiǎn)單而一致的方式來訪問集合中的元素,而不需要暴露集合內(nèi)部的結(jié)構(gòu),這篇文章主要介紹了Java語言Iterator轉(zhuǎn)換成 List的方法,需要的朋友可以參考下2023-08-08
spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)操作
這篇文章主要介紹了spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

