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

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

3、創(chuàng)建maven項目
4、在pom.xml導入依賴包
<!-- java發(fā)送郵件jar包 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
5、創(chuàng)建java類 類名取為:SendEmailManger(注意包別導錯了)
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ā)送郵件,需要進行身份驗證
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// smtp登陸的賬號、密碼 ;需開啟smtp登陸
props.setProperty("mail.debug", "true");
props.put("mail.user", "發(fā)送者郵箱");
props.put("mail.password", "授權(quán)碼");
// 特別需要注意,要將ssl協(xié)議設置為true,否則會報530錯誤
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)建郵件會話
Session mailSession = Session.getInstance(props, authenticator);
// 創(chuàng)建郵件消息
MimeMessage message = new MimeMessage(mailSession);
// 設置發(fā)件人
try {
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 設置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(Message.RecipientType.TO, to);
// 設置抄送
// InternetAddress cc = new InternetAddress("591566764@qq.com");
// message.setRecipient(RecipientType.CC, cc);
// 設置密送,其他的收件人不能看到密送的郵件地址
// InternetAddress bcc = new InternetAddress("mashen@163.com");
// message.setRecipient(RecipientType.CC, bcc);
// 設置郵件標題
message.setSubject(subject);
// 設置郵件的內(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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security SecurityContextHolder組件示例說明
SpringSecurity的SecurityContextHolder組件是存儲當前安全上下文的地方,包括認證用戶信息,它支持全局訪問、線程局部存儲和上下文傳播,是SpringSecurity認證和授權(quán)的核心,文章通過示例展示了如何訪問已認證用戶的詳細信息、手動設置認證信息以及使用認證信息保護方法2024-11-11
Mybatis之Select Count(*)的獲取返回int的值操作
這篇文章主要介紹了Mybatis之Select Count(*)的獲取返回int的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
springboot 啟動時初始化數(shù)據(jù)庫的步驟
這篇文章主要介紹了springboot 啟動時初始化數(shù)據(jù)庫的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
Java并發(fā)源碼分析ConcurrentHashMap線程集合
這篇文章主要為大家介紹了Java并發(fā)源碼分析ConcurrentHashMap線程集合,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

