java.mail實(shí)現(xiàn)發(fā)送郵件
本文實(shí)例為大家分享了java.mail實(shí)現(xiàn)發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
前提條件:
需要對(duì)郵箱做出設(shè)置,開(kāi)啟 :
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV等服務(wù)
一方面是接收這些代理服務(wù)器發(fā)送的郵件
另一方面就是讓郵箱接受這些郵件協(xié)議的代理
在登錄的時(shí)候,就是用戶名和授權(quán)碼,進(jìn)行登錄
獲取授權(quán)碼的過(guò)程:直接百度搜索就有結(jié)果,這里不再贅述
demo
這里需要引入javax.mail jar包

package test;
///**
// *
// * @author jingxl0327
// * @Description 郵件發(fā)送測(cè)試
// */
public class sendMailTest {
public static void main(String[] args) throws Exception {
// 配置信息
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.163.com");
pro.put("mail.smtp.auth", "true");
// SSL加密
MailSSLSocketFactory sf = null;
sf = new MailSSLSocketFactory();
// 設(shè)置信任所有的主機(jī)
sf.setTrustAllHosts(true);
pro.put("mail.smtp.ssl.enable", "true");
pro.put("mail.smtp.ssl.socketFactory", sf);
// 根據(jù)郵件的會(huì)話屬性構(gòu)造一個(gè)發(fā)送郵件的Session,這里需要注意的是用戶名那里不能加后綴,否則便不是用戶名了
//還需要注意的是,這里的密碼不是正常使用郵箱的登陸密碼,而是客戶端生成的另一個(gè)專門(mén)的授權(quán)碼
MailAuthenticator authenticator = new MailAuthenticator("jingxl0327",
"jing0327");
Session session = Session.getInstance(pro, authenticator);
// 根據(jù)Session 構(gòu)建郵件信息
Message message = new MimeMessage(session);
// 創(chuàng)建郵件發(fā)送者地址
Address from = new InternetAddress("jingxl0327@163.com");
// 設(shè)置郵件消息的發(fā)送者
message.setFrom(from);
// 驗(yàn)證收件人郵箱地址
List<String> toAddressList = new ArrayList<>();
toAddressList.add("1157496573@qq.com");
StringBuffer buffer = new StringBuffer();
if (!toAddressList.isEmpty()) {
String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(regEx);
for (int i = 0; i < toAddressList.size(); i++) {
Matcher match = p.matcher(toAddressList.get(i));
if (match.matches()) {
buffer.append(toAddressList.get(i));
if (i < toAddressList.size() - 1) {
buffer.append(",");
}
}
}
}
String toAddress = buffer.toString();
if (!toAddress.isEmpty()) {
// 創(chuàng)建郵件的接收者地址
Address[] to = InternetAddress.parse(toAddress);
// 設(shè)置郵件接收人地址
message.setRecipients(Message.RecipientType.TO, to);
// 郵件主題
message.setSubject("這個(gè)是主題");
// 郵件容器
MimeMultipart mimeMultiPart = new MimeMultipart();
// 設(shè)置HTML
BodyPart bodyPart = new MimeBodyPart();
// 郵件內(nèi)容
String htmlText = "這個(gè)是內(nèi)容";
bodyPart.setContent(htmlText, "text/html;charset=utf-8");
mimeMultiPart.addBodyPart(bodyPart);
// 添加附件
List<String> fileAddressList = new ArrayList<String>();
fileAddressList
.add("C:\\Users\\tuzongxun123\\Desktop\\新建 Microsoft Office Word 文檔.docx");
if (fileAddressList != null) {
BodyPart attchPart = null;
for (int i = 0; i < fileAddressList.size(); i++) {
if (!fileAddressList.get(i).isEmpty()) {
attchPart = new MimeBodyPart();
// 附件數(shù)據(jù)源
DataSource source = new FileDataSource(
fileAddressList.get(i));
// 將附件數(shù)據(jù)源添加到郵件體
attchPart.setDataHandler(new DataHandler(source));
// 設(shè)置附件名稱為原文件名
attchPart.setFileName(MimeUtility.encodeText(source
.getName()));
mimeMultiPart.addBodyPart(attchPart);
}
}
}
message.setContent(mimeMultiPart);
message.setSentDate(new Date());
// 保存郵件
message.saveChanges();
// 發(fā)送郵件
Transport.send(message);
}
}
}
class MailAuthenticator extends Authenticator {
/**
* 用戶名
*/
private String username;
/**
* 密碼
*/
private String password;
/**
* 創(chuàng)建一個(gè)新的實(shí)例 MailAuthenticator.
*
* @param username
* @param password
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例)
這篇文章主要介紹了java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Spring Security 中如何讓上級(jí)擁有下級(jí)的所有權(quán)限(案例分析)
這篇文章主要介紹了Spring Security 中如何讓上級(jí)擁有下級(jí)的所有權(quán)限,本文通過(guò)案例分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java中關(guān)于int和Integer的區(qū)別詳解
本篇文章小編為大家介紹,在Java中 關(guān)于int和Integer的區(qū)別詳解,需要的朋友參考下2013-04-04
Spring-retry實(shí)現(xiàn)循環(huán)重試功能
這篇文章主要介紹了Spring-retry 優(yōu)雅的實(shí)現(xiàn)循環(huán)重試功能,通過(guò)@Retryable注解,優(yōu)雅的實(shí)現(xiàn)循環(huán)重試功能,需要的朋友可以參考下2023-07-07
高并發(fā)環(huán)境下安全修改同一行數(shù)據(jù)庫(kù)數(shù)據(jù)的策略分享
隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,越來(lái)越多的應(yīng)用需要在高并發(fā)環(huán)境中運(yùn)行,數(shù)據(jù)庫(kù)的并發(fā)控制成為了業(yè)務(wù)的關(guān)鍵,本文將介紹如何在高并發(fā)情況下,安全地修改數(shù)據(jù)庫(kù)中的同一行數(shù)據(jù),需要的可以參考一下2023-06-06
Java Set接口及常用實(shí)現(xiàn)類總結(jié)
Collection的另一個(gè)子接口就是Set,他并沒(méi)有我們List常用,并且自身也沒(méi)有一些額外的方法,全是繼承自Collection中的,因此我們還是簡(jiǎn)單總結(jié)一下,包括他的常用實(shí)現(xiàn)類HashSet、LinkedHashSet、TreeSet的總結(jié)2023-01-01
SpringBoot自定義MessageConverter與內(nèi)容協(xié)商管理器contentNegotiationManag
這篇文章主要介紹了SpringBoot自定義MessageConverter與內(nèi)容協(xié)商管理器contentNegotiationManager的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10

