SpringBoot integrates SpringEmail to send emails in batches
Preface: The company currently has a business that is to send emails to subscribers, so I write down the content of this section
1. Introduce dependencies
<! -- email--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>Copy the code
2. Set the email address
3. Write configuration information to application
Spring: mail: host: smtp.163.com #SMTP server address username: ******* # login account password: ******* # login password (or authorization code) default-encoding: Utf-8 properties: from: ******* # Mail sender mail: SMTP: auth: true starTTls: enable: true required: trueCopy the code
==smtp.exmail.qq.com is a QQ mailbox. If you use netease mailbox, you can write smtp.163.com==
4. The implementation class
@Service
public class MailServiceImpl extends MailService {
@Value("${spring.mail.properties.from}")
private String mallFrom;
@Resource
private JavaMailSender javaMailSender;
@Override
public String emailMessage(String title, String content) {
// Query the email list
List<String> emailList = ;
// Send it to the mailbox
try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// Set the sender's Email
simpleMailMessage.setFrom(mallFrom);
// Set the email subject
simpleMailMessage.setSubject(title);
// Set the email subject
simpleMailMessage.setText(content);
// Batch sendString[] emailArray = emailList.stream().. toArray(String[]::new);
javaMailSender.send(simpleMailMessage);
} catch (Exception e) {
log.error("SendEmailMessage error: ", e);
throw new MallBusinessException("Failed to send message:");
}
return "ok";
}
Copy the code
Sent successfully!!
The reason for this is because there are two apis in the SimpleMailMessage class that set the sending mailbox, and one of thethis.to
Is an array object,SetTo arrays can be sent in batches
##### == pass by a praise bar, to meet the vanity! = =