JavaMail郵件發(fā)送機(jī)制詳解
這篇文章主要介紹了JavaMail郵件發(fā)送機(jī)制詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
概念
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>", true); //指定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>
3.mail.properties文件:
mail.host=smtp.163.com mail.username=@前面的用戶名 mail.password=密碼 mail.from=發(fā)件人郵箱全拼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaMail實(shí)現(xiàn)郵件發(fā)送機(jī)制
- JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能
- JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
- 基于JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
- 基于JavaMail的Java郵件發(fā)送
- 基于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)文章
Springboot錯(cuò)誤頁面和錯(cuò)誤信息定制操作
這篇文章主要介紹了Springboot錯(cuò)誤頁面和錯(cuò)誤信息定制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java使用Math.random()結(jié)合蒙特卡洛方法計(jì)算pi值示例
這篇文章主要介紹了Java使用Math.random()結(jié)合蒙特卡洛方法計(jì)算pi值的方法,簡(jiǎn)單說明了結(jié)合具體實(shí)例蒙特卡洛方法的原理,并結(jié)合具體實(shí)例形式分析了java使用蒙特卡洛方法計(jì)算PI值的操作技巧,需要的朋友可以參考下2017-09-09java實(shí)現(xiàn)XML增加元素操作簡(jiǎn)單示例
這篇文章主要介紹了java實(shí)現(xiàn)XML增加元素操作,結(jié)合簡(jiǎn)單實(shí)例形式分析了java針對(duì)xml格式數(shù)據(jù)的讀取、遍歷、創(chuàng)建等操作技巧,需要的朋友可以參考下2017-02-02淺析Spring boot 中 logback 配置<springPropert
這篇文章主要介紹了淺析Spring boot 中 logback 配置<springProperty> 讀取application.properties 中的屬性,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02springboot基于過濾器實(shí)現(xiàn)接口請(qǐng)求耗時(shí)統(tǒng)計(jì)操作
這篇文章主要介紹了springboot基于過濾器實(shí)現(xiàn)接口請(qǐng)求耗時(shí)統(tǒng)計(jì)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09