使用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)文章
nodejs連接dubbo服務(wù)的java工程實(shí)現(xiàn)示例
這篇文章主要介紹了在項(xiàng)目遷移中,nodejs連接dubbo服務(wù)的java工程實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
SpringBoot 如何從配置文件讀取值到對(duì)象中
這篇文章主要介紹了SpringBoot 如何從配置文件讀取值到對(duì)象中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
詳解Java使用super和this來(lái)重載構(gòu)造方法
這篇文章主要介紹了詳解Java使用super和this來(lái)重載構(gòu)造方法的相關(guān)資料,這里提供實(shí)例來(lái)幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Hibernate雙向多對(duì)多映射關(guān)系配置代碼實(shí)例
這篇文章主要介紹了Hibernate雙向多對(duì)多映射關(guān)系配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java?項(xiàng)目連接并使用?SFTP?服務(wù)的示例詳解
SFTP是一種安全的文件傳輸協(xié)議,是SSH(Secure?Shell)協(xié)議的一個(gè)子協(xié)議,設(shè)計(jì)用于加密和保護(hù)文件傳輸?shù)陌踩?這篇文章主要介紹了Java?項(xiàng)目如何連接并使用?SFTP?服務(wù)的示例詳解,需要的朋友可以參考下2025-01-01
Java數(shù)據(jù)結(jié)構(gòu)最清晰圖解二叉樹(shù)前 中 后序遍歷
樹(shù)是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹(shù)中稱為結(jié)點(diǎn))按分支關(guān)系組織起來(lái)的結(jié)構(gòu),很象自然界中的樹(shù)那樣。樹(shù)結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹(shù)形象表示2022-01-01
springboot省去配置Tomcat的步驟問(wèn)題
這篇文章主要介紹了springboot省去配置Tomcat的步驟問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

