基于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: 編寫測試類:
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值一樣.
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring Boot 自定義PropertySourceLoader
這篇文章主要介紹了詳解Spring Boot 自定義PropertySourceLoader,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05詳解java實(shí)現(xiàn)簡單掃碼登錄功能(模仿微信網(wǎng)頁版掃碼)
這篇文章主要介紹了java實(shí)現(xiàn)簡單掃碼登錄功能(模仿微信網(wǎng)頁版掃碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05IDEA 插件 mapper和xml互相跳轉(zhuǎn)操作
這篇文章主要介紹了IDEA 插件 mapper和xml互相跳轉(zhuǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02