不好用请移至评论区揍我
原创代码,请勿转载,谢谢!
一、介绍
- 用于给用户发送特定的邮件内容,支持附件、批量发送
- 邮箱账号必须要开启 SMTP 服务(具体见下文教程)
- 本文邮箱设置示例以”网易邮箱“为例,其他如qq邮箱或企业邮箱均可,只要在设置中对应开启SMTP及授权码等操作即可使用
- 完整代码见文末
二、邮箱设置
开启 SMTP 服务
设置授权码
三、完整代码
maven
<dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.4.7</version>
</dependency>
java
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;/*** @Author 954* @create 2024/1/27 13:46*/
public class EmailUtil {// 发送者别名private static final String SENDER_NAME = "XXX" ;// 发送邮箱地址private static final String SENDER_ADDRESS = "XXX@163.com" ;// 发送邮箱的授权码private static final String SENDER_PWD = "XXX" ;// 密送的邮箱地址private static final String PRIVATE_ADDRESS = "XXX@163.com" ;/*** 发送邮件的环境对象*/private static final Session EMAIL_SESSION = getEmailSession();/*** 批量发送电子邮件* @param emailAddressList 邮箱地址* @param content 邮件内容* @param title 邮件标题* @param fileList 附件* @throws Exception*/public synchronized void sendEmail(List<String> emailAddressList, String title, String content, List<File> fileList) throws Exception {MimeMessage mimeMessage = getMimeMessage(emailAddressList, title, content);if (!CollectionUtils.isEmpty(fileList)){// 处理附件Multipart multipart = getMultipart(fileList);mimeMessage.setContent(multipart);// 添加邮件内容BodyPart contentPart = new MimeBodyPart();contentPart.setContent(content, "text/html;charset=UTF-8");// 将multipart对象放入message中multipart.addBodyPart(contentPart);}Transport.send(mimeMessage);}private MimeMessage getMimeMessage(List<String> emailAddressList, String title, String content) throws Exception {// 创建邮件消息MimeMessage message = new MimeMessage(EMAIL_SESSION);// 设置发件人message.setFrom(new InternetAddress(SENDER_ADDRESS, SENDER_NAME));// 设置收件人InternetAddress[] address = new InternetAddress[emailAddressList.size()] ;for (int i = 0; i < emailAddressList.size(); i++){address[i] = new InternetAddress(emailAddressList.get(i)) ;}message.setRecipients(Message.RecipientType.TO, address);// 设置密送message.setRecipient(Message.RecipientType.BCC, new InternetAddress(PRIVATE_ADDRESS));// 设置邮件标题message.setSubject(title, "UTF-8");// 设置邮件的内容体message.setContent(content, "text/html;charset=UTF-8");// 设置发送时间message.setSentDate(new Date());return message;}private Multipart getMultipart(List<File> fileList) {if (CollectionUtils.isEmpty(fileList)) return null;Multipart multipart = new MimeMultipart();// 添加附件的内容fileList.stream().parallel().forEach(file -> {try {BodyPart attachmentBodyPart = new MimeBodyPart();DataSource source = new FileDataSource(file);attachmentBodyPart.setDataHandler(new DataHandler(source));// MimeUtility.encodeWord可以避免文件名乱码attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));multipart.addBodyPart(attachmentBodyPart);} catch (Exception e) {e.printStackTrace();}});return multipart ;}private static Session getEmailSession(){// 配置发送邮件的环境属性Properties props = new Properties();//设置用户的认证方式props.setProperty("mail.smtp.auth", "true");//设置传输协议props.setProperty("mail.transport.protocol", "smtp");//设置发件人的SMTP服务器地址props.setProperty("mail.smtp.host", "smtp.163.com");// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码return new PasswordAuthentication(SENDER_ADDRESS, SENDER_PWD);}};return Session.getInstance(props, authenticator);}public static void main(String[] args) throws Exception {sendEmail(Arrays.asList("XXX@qq.com", "XXX@163.com"), "我是标题", "我是内容", null);}}