首頁 >Java >java教程 >基於Java如何實作簡單的郵件群發功能?

基於Java如何實作簡單的郵件群發功能?

王林
王林轉載
2023-04-20 16:28:082021瀏覽

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() { }
 
    /**
     * 群发邮件方法
     */
    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);
    }
 
    /**
     * 发送邮件
     */
    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("今天你学习了么?");
        simpleEmail.setContent("今天你写博客了么");
        send("1334512682@qq.com", simpleEmail);
    }
}

email.properties 系統郵箱設定

email.username=
@163 .com

email.password=

你的郵件帳號和密碼,也可以省去設定文件,直接把帳號密碼寫死在程式碼。

執行測試

右鍵run 執行主方法。

基於Java如何實作簡單的郵件群發功能?

基於Java如何實作簡單的郵件群發功能?

將傳送的郵件信箱綁定到微信上,還能實作微信提醒功能!

基於Java如何實作簡單的郵件群發功能?

#######

以上是基於Java如何實作簡單的郵件群發功能?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除