基于Javamail實(shí)現(xiàn)發(fā)送郵件(QQ/網(wǎng)易郵件服務(wù)器)
本文實(shí)例為大家分享了Javamail實(shí)現(xiàn)發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
一. 使用QQ郵箱作為smtp郵件服務(wù)器發(fā)送郵件
步驟1.開啟QQ郵箱的POP3/SMTP服務(wù):

開啟后會(huì)得到一個(gè)16位授權(quán)碼, 作為第三方使用郵件服務(wù)器的登錄憑證.
注意: 修改郵箱密碼后, 授權(quán)碼會(huì)失效, 需要重新獲取.
步驟2: 編寫配置文件applicationContext-email.xml(此處使用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:tx="http://www.springframework.org/schema/tx" ?? ?xmlns:context="http://www.springframework.org/schema/context" ?? ?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/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 ?? ??? ?http://www.springframework.org/schema/aop ?? ??? ?http://www.springframework.org/schema/aop/spring-aop.xsd" ?? ?default-lazy-init="false"> ?? ?<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> ?? ??? ?<property name="host"> ?? ??? ??? ?<!-- qq的SMTP郵箱服務(wù)器地址, 若使用163網(wǎng)易則改為:smtp.163.com --> ?? ??? ??? ?<value>smtp.qq.com</value> ?? ??? ?</property> <!-- ?? ?<property name="port"> ?? ??? ??? ?SMTP郵箱服務(wù)器端口(465或587), 建議不要配置, 使用默認(rèn)就行? ?? ??? ??? ?<value>建議不要配置!!博主配置反而發(fā)布出去!!</value> ?? ??? ?</property> --> ?? ??? ?<property name="javaMailProperties"> ?? ??? ??? ?<props> ?? ??? ??? ??? ?<prop key="mail.smtp.auth">true</prop> ?? ??? ??? ??? ?<!-- 連接超時(shí)時(shí)間 --> ?? ??? ??? ??? ?<prop key="mail.smtp.timeout">25000</prop> ?? ??? ??? ?</props> ?? ??? ?</property> ?? ??? ?<!-- 你的郵箱賬號(hào) --> ?? ??? ?<property name="username"> ?? ??? ??? ?<value>xxxxxxx@qq.com</value> ?? ??? ?</property> ?? ??? ?<!-- 16位授權(quán)碼, 注意不是登錄密碼! --> ?? ??? ?<property name="password"> ?? ??? ??? ?<value>qazcrslpoghcbahh</value> ?? ??? ?</property> ?? ??? ?<property name="defaultEncoding"> ?? ??? ??? ?<value>UTF-8</value> ?? ??? ?</property> ?? ?</bean> ?? ?<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> ?? ??? ?<!-- 發(fā)件人信息, 需要和上面username值一樣 --> ?? ??? ?<property name="from" value="xxxxxxx@qq.com" /> ?? ?</bean> </beans>
步驟3: 編寫測(cè)試類:
package emailtest;
import java.util.Date;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-email.xml")
public class EmailTest {
?? ?@Resource
?? ?private JavaMailSender javaMailSender;
?? ?@Resource
?? ?private SimpleMailMessage simpleMailMessage;
?? ?
?? ?@Test
?? ?public void sendMail() throws MessagingException{
?? ??? ?sendMail("xxxxx@163.com","驗(yàn)證碼:6666","密碼找回");
?? ?}
?? ?
?? ?public void sendMail(String email, String content, String subject) throws MessagingException {
?? ??? ?MimeMessage message = javaMailSender.createMimeMessage();
?? ??? ?MimeMessageHelper messageHelper;
?? ??? ?messageHelper = new MimeMessageHelper(message, true, "UTF-8");
?? ??? ?messageHelper.setFrom(StringUtils.trimAllWhitespace(simpleMailMessage.getFrom()));
?? ??? ?messageHelper.setTo(email);
?? ??? ?messageHelper.setSubject(subject);
?? ??? ?messageHelper.setText(content, true);
?? ??? ?messageHelper.setSentDate(new Date());
?? ??? ?// 發(fā)送郵件
?? ??? ?javaMailSender.send(messageHelper.getMimeMessage());
?? ??? ?
?? ?}
}二. 使用網(wǎng)易郵箱作為smtp郵件服務(wù)器發(fā)送郵件
1.相似的, 先打開網(wǎng)易郵箱的POP3/SMTP服務(wù), 設(shè)置授權(quán)碼.

2.修改上述applicationContext.xml中配置信息:
服務(wù)器地址改為smtp.163.com
username更改為你的網(wǎng)易郵箱賬號(hào)
password則是你在開啟POP3/SMTP服務(wù)時(shí)設(shè)置的授權(quán)碼
from的值和username值一樣.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程
最近一段時(shí)間搭建了ssm環(huán)境,并測(cè)試了幾個(gè)小項(xiàng)目,下面小編通過圖文并茂的形式給大家分享Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程,需要的朋友參考下吧2017-11-11
Spring?Data?JPA框架的Repository自定義實(shí)現(xiàn)詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的Repository自定義實(shí)現(xiàn)2022-04-04
maven將項(xiàng)目打包上傳到nexus私服的詳細(xì)教程
這篇文章主要介紹了maven將項(xiàng)目打包上傳到nexus私服,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07
SpringBoot整合第三方技術(shù)的詳細(xì)步驟
這篇文章主要介紹了SpringBoot整合第三方技術(shù),包括整合Junit、整合Mybatis、整合Mybatis-plus、整合Druid,本文通過實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
為什么Spring官方推薦的@Transational還能導(dǎo)致生產(chǎn)事故
在Spring中進(jìn)行事務(wù)管理非常簡(jiǎn)單,只需要在方法上加上注解@Transactional,那么為什么Spring官方推薦的@Transational還能導(dǎo)致生產(chǎn)事故,本文就詳細(xì)的介紹一下2021-11-11
java多線程抓取鈴聲多多官網(wǎng)的鈴聲數(shù)據(jù)
很容易就能發(fā)現(xiàn)通過改變 listId和page就能從服務(wù)器獲取鈴聲的json數(shù)據(jù), 通過解析json數(shù)據(jù), 可以看到都帶有{"hasmore":1,"curpage":1}這樣子的指示,通過判斷hasmore的值,決定是否進(jìn)行下一頁的抓取。 但是通過上面這個(gè)鏈接返回的json中不帶有鈴聲的下載地址2016-04-04
ZooKeeper入門教程二在單機(jī)和集群環(huán)境下的安裝搭建及使用
本文是ZooKeeper入門系列教程,涵蓋ZooKeeper的安裝使及單機(jī)集群環(huán)境搭建,通過實(shí)例和大量圖表,結(jié)合實(shí)戰(zhàn),幫助學(xué)習(xí)者理解和運(yùn)用,有需要的朋友可以借鑒參考下2022-01-01

