基于Java實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能
pom文件引入第三方依賴
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
java代碼如下
import lombok.Data;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Created by tarzan liu on 2021/5/9.
*/
public abstract class EmailUtil {
private static final Session session;
private static final EmailAuthenticator authenticator;
static {
InputStream inputStream = null;
try {
inputStream = EmailUtil.class.getResourceAsStream("/email.properties");
Properties properties = new Properties();
properties.load(inputStream);
authenticator = new EmailAuthenticator();
String username = properties.getProperty("email.username");
authenticator.setUsername(username);
String password = properties.getProperty("email.password");
authenticator.setPassword(password);
String smtpHostName = "smtp." + username.split("@")[1];
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", smtpHostName);
session = Session.getInstance(properties, authenticator);
} catch (Exception e) {
throw new RuntimeException("init error.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private EmailUtil() { }
/**
* 群發(fā)郵件方法
*/
private static void massSend(List<String> recipients, SimpleEmail email) throws MessagingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(authenticator.getUsername()));
InternetAddress[] addresses = new InternetAddress[recipients.size()];
for (int index = 0; index < recipients.size(); index ++) {
addresses[index] = new InternetAddress(recipients.get(index));
}
message.setRecipients(RecipientType.TO, addresses);
message.setSubject(email.getSubject());
message.setContent(email.getContent(), "text/html;charset=utf-8");
Transport.send(message);
}
/**
* 發(fā)送郵件
*/
public static void send(String recipient, SimpleEmail email) throws MessagingException {
List<String> recipients = new ArrayList<>();
recipients.add(recipient);
massSend(recipients, email);
}
//可以單獨(dú)建一個(gè)類
@Data
public static class SimpleEmail {
private String subject;
private String content;
}
public static void main(String[] args) throws Exception {
SimpleEmail simpleEmail = new SimpleEmail();
simpleEmail.setSubject("今天你學(xué)習(xí)了么?");
simpleEmail.setContent("今天你寫博客了么");
send("1334512682@qq.com", simpleEmail);
}
}email.properties 系統(tǒng)郵箱配置
email.username=###@163.com
email.password=###
你的郵箱賬號(hào)和密碼,也可以省去配置文件,直接把賬號(hào)密碼寫死在代碼。
運(yùn)行測(cè)試
右鍵run 運(yùn)行主方法。


將發(fā)送的郵箱綁定到微信上,還能實(shí)現(xiàn)微信提醒功能!

到此這篇關(guān)于基于Java實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能的文章就介紹到這了,更多相關(guān)Java郵件群發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis學(xué)習(xí)筆記之mybatis注解配置詳解
本篇文章主要介紹了mybatis學(xué)習(xí)筆記之mybatis注解配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java自帶消息隊(duì)列Queue的使用教程詳細(xì)講解
這篇文章主要介紹了Java自帶消息隊(duì)列Queue的使用教程,Java中的queue類是隊(duì)列數(shù)據(jù)結(jié)構(gòu)管理類,在它里邊的元素可以按照添加它們的相同順序被移除,隊(duì)列通常以FIFO的方式排序各個(gè)元素,感興趣想要詳細(xì)了解可以參考下文2023-05-05
教您如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能
對(duì)于EasyExcel庫(kù),我們可以使用它來(lái)實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入和導(dǎo)出,下面這篇文章主要給大家介紹了關(guān)于如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a
這篇文章主要介紹了Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Spring中如何獲取request的方法匯總及其線程安全性分析
這篇文章主要給大家介紹了關(guān)于Spring中如何獲取request的方法匯總及其線程安全性分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
MyBatis-Plus Sequence主鍵的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringDataJpa如何使用union多表分頁(yè)條件查詢
這篇文章主要介紹了SpringDataJpa如何使用union多表分頁(yè)條件查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

