基于Java實現(xià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);
}
//可以單獨建一個類
@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=###
你的郵箱賬號和密碼,也可以省去配置文件,直接把賬號密碼寫死在代碼。
運行測試
右鍵run 運行主方法。


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

到此這篇關(guān)于基于Java實現(xiàn)簡單的郵件群發(fā)功能的文章就介紹到這了,更多相關(guān)Java郵件群發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過實例學(xué)習(xí)JAVA對象轉(zhuǎn)成XML輸出
這篇文章主要介紹了通過實例學(xué)習(xí)JAVA對象轉(zhuǎn)成XML輸出,做流程圖的項目時,新的流程定義為xml的,需要對xml與java對象進行互轉(zhuǎn),下面我們來深入學(xué)習(xí),需要的朋友可以參考下2019-06-06
JDK8 new ReentrantLock((true)加鎖流程
這篇文章主要介紹了java面試中常遇到的問題JDK8 new ReentrantLock((true)加鎖流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
SpringBoot 異步線程間數(shù)據(jù)傳遞的實現(xiàn)
本文主要介紹了SpringBoot 異步線程間數(shù)據(jù)傳遞的實現(xiàn),包括異步線程的基本概念、數(shù)據(jù)傳遞的方式、具體實現(xiàn)方式等,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Springboot實現(xiàn)多線程注入bean的工具類操作
這篇文章主要介紹了Springboot實現(xiàn)多線程注入bean的工具類操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Java+opencv3.2.0實現(xiàn)hough直線檢測
這篇文章主要為大家詳細介紹了Java+opencv3.2.0之hough直線檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

