溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能

發(fā)布時(shí)間:2022-05-10 13:46:07 來源:億速云 閱讀:223 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

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)行主方法。

基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能

基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能

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

基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能

以上就是“基于Java怎么實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI