This article has shared the specific code for Java to implement mass email for your reference. The specific content is as follows
I have nothing to do recently. I have read some great articles online, and I saw one of them better. I will share them with you!
Below is the code
Email entity
import java.io.Serializable; /** * Mail entity class*/ public class Mail implements Serializable { /** * Serial number*/ private static final long serialVersionUID = -3562218214168975242L; /** * Mail codes */ public static final String ENCODEING = "UTF-8"; /** * Server address*/ private String host; /** * Server port number*/ private String portNumber; /** * Sender's email*/ private String sender; /** * Recipient's email*/ private String receiver; /** * Sender nickname*/ private String name; /** * Account*/ private String username; /** * Password*/ private String password; /** * Subject*/ private String subject; /** * Information (support HTML) */ private String message; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getSender() { return sender; } public String getPortNumber() { return portNumber; } public void setPortNumber(String portNumber) { this.portNumber = portNumber; } public void setSender(String sender) { this.sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } Tools
import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; public class MailUtil { public boolean send(Mail mail) { //Send email object HtmlEmail email = new HtmlEmail(); try { //Here is the name of the SMTP sending server email.setHostName(mail.getHost()); //When the port number is not empty, the user-defined port number is SMTP sending server port number if (!"".equals(mail.getPortNumber())) { email.setSSLOnConnect(true); email.setSslSmtpPort(mail.getPortNumber()); } //Set the character encoding set email.setCharset(Mail.ENCODEING); //Recipient's email.addTo(mail.getReceiver()); //Sendor's email.setFrom(mail.getSender(), mail.getName()); //If authentication information is required, set authentication: username-password. The sender's registration name and password on the mail server are respectively email.setAuthentication(mail.getUsername(), mail.getPassword()); // The email subject to send email.setSubject(mail.getSubject()); // The message to send, since HtmlEmail is used, you can use the HTML tag email.setMsg(mail.getMessage()); // Send email.send(); return true; } catch (EmailException e) { e.printStackTrace(); return false; } } } start
import java.util.Random; public class SimpleEmailClient { public static void main(String[] args) throws InterruptedException { Mail mail = new Mail(); mail.setHost("smtp.qq.com"); // Set the mail server. If you do not use the QQ mailbox, find the relevant mail.setPortNumber("465"); // Set the mail server port number, default 25 mail.setSender("[email protected]"); // Sender mail.setName("Yang Daxia"); // The sender's nickname mail.setSubject("delicious and delicious"); //Send the subject mail.setMessage("delicious and delicious"); //Send the content mail.setUsername("[email protected]"); // Log in to the account, usually the same as the email name mail.setPassword("*********"); // When logging in to a third-party client in QQ mailbox, please enter the "authorization code" for verification. For other passwords, please check the instructions for the mail server for (int i = 0; i < 1000; i++) { //Thread.sleep(2000); int max1 = 99999; int min1 = 10000; Random random = new Random(); int f = random.nextInt(max1)%(max1-min1+1) + min1; int max2 = 9999; int min2 = 1000; Random random2 = new Random(); int s = random2.nextInt(max2)%(max2-min2+1) + min2; String account = "" + f + "" + s + "@qq.com"; mail.setReceiver(account); // Receiver System.out.println(account); if (new MailUtil().send(mail)) { System.out.println("Send successfully"); } else { System.out.println("Send failed"); } } } } }For loop is for mass
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.