java is used to realize the function of group email
- 2020-12-09 00:52:03
- OfStack
The example of this paper shares the specific code of java group email for your reference. The specific content is as follows
Recently nothing, on the Internet to see some of the cattle article, see one of the better, share to everyone!
Here's the code
Email entity
import java.io.Serializable;
/**
* Mail entity class
*/
public class Mail implements Serializable {
/**
* The serial number
*/
private static final long serialVersionUID = -3562218214168975242L;
/**
* Mail code s
*/
public static final String ENCODEING = "UTF-8";
/**
* Server address
*/
private String host;
/**
* Server port number
*/
private String portNumber;
/**
* Sender's email address
*/
private String sender;
/**
* Recipient's Email address
*/
private String receiver;
/**
* Sender nickname
*/
private String name;
/**
* account
*/
private String username;
/**
* password
*/
private String password;
/**
* The theme
*/
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;
}
}
Utility class
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 SMTP Name of the sending server
email.setHostName(mail.getHost());
// When the port number is not empty , The user-defined port number is SMTP Send server port number
if (!"".equals(mail.getPortNumber())) {
email.setSSLOnConnect(true);
email.setSslSmtpPort(mail.getPortNumber());
}
// The setting of a character encoding set
email.setCharset(Mail.ENCODEING);
// Recipient's Email address
email.addTo(mail.getReceiver());
// The sender's mailbox
email.setFrom(mail.getSender(), mail.getName());
// If authentication information is required, set authentication: user name - The password. For the sender's registered name and password on the mail server, respectively
email.setAuthentication(mail.getUsername(), mail.getPassword());
// The subject of the message to be sent
email.setSubject(mail.getSubject());
// The message to be sent due to the use of HtmlEmail Can be used in the content of an email HTML The label
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 up the mail server , If it's not QQ The email address , Find something relevant for yourself
mail.setPortNumber("465"); // Set the mail server port number , The default 25
mail.setSender("xxxx@qq.com"); // The sender
mail.setName(" heroyang "); // Sender's nickname
mail.setSubject(" Yummy "); // Send theme
mail.setMessage(" Yummy "); // Send content
mail.setUsername("xxxx@qq.com"); // Login account ,1 It's usually an email address 1 sample
mail.setPassword("********"); // QQ Mailbox login control 3 When the side client , Please enter "authorization code" in the password box for verification. For other passwords, see the instructions on 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); // recipient
System.out.println(account);
if (new MailUtil().send(mail)) {
System.out.println(" Send a success ");
} else {
System.out.println(" Send failure ");
}
}
}
}
The for loop is for mass hair