How to realize the simple function of mass mail sending based on Java
Today, the editor will share with you the relevant knowledge points about how to achieve simple mass email based on Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Pom files introduce third-party dependencies on javax.mail mail 1.4 org.projectlombok lombok The true java code is as follows: 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 () {} / * Group email method * / private static void massSend (List 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) } / * send email * / public static void send (String recipient, SimpleEmail email) throws MessagingException {List recipients = new ArrayList (); recipients.add (recipient); massSend (recipients, email);} / / you can create a separate class @ 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 ("have you studied today?") ; simpleEmail.setContent ("have you written a blog today"); send ("1334512682@qq.com", simpleEmail);}}
Email.properties system mailbox configuration
Email.username=###@163.com
Email.password=###
Your email account and password, you can also omit the configuration file, directly write the account password in the code.
Run the test
Right-click run to run the main method.
Will send the mailbox bound to Wechat, but also can achieve Wechat reminder function!
These are all the contents of the article "how to achieve simple mass email based on Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.