使用Java實(shí)現(xiàn)qq郵箱發(fā)送郵件
本文實(shí)例為大家分享了Java操作qq郵箱發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
今天嘗試了使用QQ郵箱的POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù)來(lái)進(jìn)行發(fā)送郵件?。ㄟ@些個(gè)服務(wù)就是些協(xié)議,只有開(kāi)啟了之后就可以做一些操作)
步驟
1、登錄QQ郵箱> 設(shè)置 > 賬戶

2、找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù)
開(kāi)啟 POP3/SMTP 服務(wù) > 拿到授權(quán)碼

3、創(chuàng)建maven項(xiàng)目
4、在pom.xml導(dǎo)入依賴包
<!-- java發(fā)送郵件jar包 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
5、創(chuàng)建java類 類名取為:SendEmailManger(注意包別導(dǎo)錯(cuò)了)
package com.xdl.util;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 郵件發(fā)送
* QQ郵箱--->別的郵箱
* @author shiyunpeng
*/
public class SendEmailManger extends Thread {
private String mailAdr;//郵箱
private String content;//郵件的內(nèi)容
private String subject;//郵件的題目
public SendEmailManger(String mailAdr, String subject, String content) {
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
@Override
public void run() {
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
//加密的郵件套接字協(xié)議工廠
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP發(fā)送郵件,需要進(jìn)行身份驗(yàn)證
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// smtp登陸的賬號(hào)、密碼 ;需開(kāi)啟smtp登陸
props.setProperty("mail.debug", "true");
props.put("mail.user", "發(fā)送者郵箱");
props.put("mail.password", "授權(quán)碼");
// 特別需要注意,要將ssl協(xié)議設(shè)置為true,否則會(huì)報(bào)530錯(cuò)誤
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用戶名、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會(huì)話
Session mailSession = Session.getInstance(props, authenticator);
// 創(chuàng)建郵件消息
MimeMessage message = new MimeMessage(mailSession);
// 設(shè)置發(fā)件人
try {
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 設(shè)置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(Message.RecipientType.TO, to);
// 設(shè)置抄送
// InternetAddress cc = new InternetAddress("591566764@qq.com");
// message.setRecipient(RecipientType.CC, cc);
// 設(shè)置密送,其他的收件人不能看到密送的郵件地址
// InternetAddress bcc = new InternetAddress("mashen@163.com");
// message.setRecipient(RecipientType.CC, bcc);
// 設(shè)置郵件標(biāo)題
message.setSubject(subject);
// 設(shè)置郵件的內(nèi)容體
message.setContent(content, "text/html;charset=UTF-8");
// 發(fā)送郵件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendEmailManger d = new SendEmailManger("接收郵件的郵箱", "syp:", "我呵呵,啊打: <br/><br/>加油哦?。。?!....");
d.start();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Solon?MVC?的?@Mapping?用法示例說(shuō)明
SolonMvc框架中的@Mapping注解用于請(qǐng)求路徑映射,支持加在public方法或類上,它可以自定義路徑、請(qǐng)求方法、內(nèi)容類型等,支持多種路徑映射表達(dá)式和參數(shù)注入方式,本文給大家介紹Solon MVC的@Mapping?用法示例說(shuō)明,感興趣的朋友一起看看吧2024-11-11
java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Spring Security SecurityContextHolder組件示例說(shuō)明
SpringSecurity的SecurityContextHolder組件是存儲(chǔ)當(dāng)前安全上下文的地方,包括認(rèn)證用戶信息,它支持全局訪問(wèn)、線程局部存儲(chǔ)和上下文傳播,是SpringSecurity認(rèn)證和授權(quán)的核心,文章通過(guò)示例展示了如何訪問(wèn)已認(rèn)證用戶的詳細(xì)信息、手動(dòng)設(shè)置認(rèn)證信息以及使用認(rèn)證信息保護(hù)方法2024-11-11
Mybatis之Select Count(*)的獲取返回int的值操作
這篇文章主要介紹了Mybatis之Select Count(*)的獲取返回int的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟
這篇文章主要介紹了springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
Java并發(fā)源碼分析ConcurrentHashMap線程集合
這篇文章主要為大家介紹了Java并發(fā)源碼分析ConcurrentHashMap線程集合,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

