今天小编给大家分享一下基于Java怎么实现简单的邮件群发功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。pom文件引入第三
今天小编给大家分享一下基于Java怎么实现简单的邮件群发功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
<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>
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; 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怎么实现简单的邮件群发功能”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。
--结束END--
本文标题: 基于Java怎么实现简单的邮件群发功能
本文链接: https://lsjlt.com/news/328790.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0