基于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)文章
mybatis學(xué)習(xí)筆記之mybatis注解配置詳解
本篇文章主要介紹了mybatis學(xué)習(xí)筆記之mybatis注解配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12教您如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能
對于EasyExcel庫,我們可以使用它來實現(xiàn)數(shù)據(jù)的導(dǎo)入和導(dǎo)出,下面這篇文章主要給大家介紹了關(guān)于如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a
這篇文章主要介紹了Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Spring中如何獲取request的方法匯總及其線程安全性分析
這篇文章主要給大家介紹了關(guān)于Spring中如何獲取request的方法匯總及其線程安全性分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04MyBatis-Plus Sequence主鍵的實現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringDataJpa如何使用union多表分頁條件查詢
這篇文章主要介紹了SpringDataJpa如何使用union多表分頁條件查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02