基于Java实现简单的邮件群发功能

2023-01-21 0 4,132

目录

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实现简单的邮件群发功能

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 基于Java实现简单的邮件群发功能 http://www.20zxx.cn/462938/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务