1. Mail transfer protocol
Email needs to be transmitted between the mail client and the mail server as well as between two mail servers. Therefore, certain rules must be observed, which is the mail transfer protocol. Here are a few protocols:
- SMTP: Short for Simple Mail Transfer Protocol. It defines the communication rules between the mail client software and the SMTP mail server, as well as between two SMTP mail servers.
- POP3: Post Office Protocol. It defines the communication rules between mail client software and POP3 mail server.
- The Internet Message Access Protocol (IMAP) is an extension of THE POP3 Protocol and defines the communication rules between the mail client software and the IMAP mail server.
2. Enable the SMTP service and obtain the authorization code
Here we take QQ mailbox as an example. In order to send QQ emails in SpringBoot, you must first turn on the SMTP function of QQ mailbox, which is disabled by default. Specific operations are as follows. Go to email → Settings → Accounts and find the one below
There’s a verification here
Once the validation is complete
3. Import dependency and configuration description
Here I’m using Gradle and introducing the spring-boot-starter-mail module
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.4.10'
Copy the code
Application. Yml configuration
Spring: mail: # host: smtp.qq.com # SMTP server port:587
username: 1786087581@qqCom # here is the email authorization code, not the email login password # here is the email authorization code, I write the password: Properties: mail: SMTP: socketFactory:class: javax.net.ssl.SSLSocketFactory# #ssl: # #enable :true# set the message code toutf- 8 -default-encoding: utf- 8 -Copy the code
Supplement: 126 Email SMTP server ADDRESS :smtp.126.com, port number :465 or 994 2163 Email SMTP server address :smtp.163.com, port number :465 or 994 Yeah email SMTP server address :smtp.yeah.net, port number :465 or 994 QQ email SMTP server address: smtp.qq.com, port number :465 or 587*
4. Email sending
4.1 Sending Simple Emails
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.util.Date;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@api (value = "email interface ",tags =" email interface ",description = "email interface ")
@RequestMapping("/mail")
public class SendMailController {
@Autowired
JavaMailSender javaMailSender;
@GetMapping("/sendMail")
public void sendMail(a){
// Build a mail object
SimpleMailMessage message = new SimpleMailMessage();
// Set the email subject
message.setSubject("This is a test email.");
// Set the sender of the message
message.setFrom("[email protected]");
// Set the mail recipient. You can have multiple recipients
message.setTo("1******[email protected]"."1*******[email protected]");
// Set email cc recipients
message.setCc("6666***[email protected]");
// Set up a private cc person
message.setBcc("l*****[email protected]");
// Set the sending date
message.setSentDate(new Date());
// Set the message body
message.setText("Test message body OK");
// Send an email
javaMailSender.send(message);
}
Copy the code
The test result is sent successfully
4.2 Sending An Email With Picture Resources
@GetMapping("/sendMailWithImg")
@apioperation (value = "produces/produces ",notes =" produces/application/json")
public void sendMailWithImg(a) throws MessagingException {
// Create a complex message
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// Set the email subject
helper.setSubject("This is a test email with pictures.");
// Set the sender of the message
helper.setFrom("[email protected]");
// Set the mail recipient
helper.setTo("1******[email protected]");
// Set the email cc
helper.setCc("6666***[email protected]");
// Set up a private cc
helper.setBcc("l*****[email protected]");
// Set the sending date
helper.setSentDate(new Date());
// Set the message body
helper.setText("< p > this is a test email with image, this E-mail contains two kinds of images, respectively, the following < / p > < p > the first picture: < / p > < img SRC = 'cid: p01' / > < p > the second image: < / p > < img SRC = 'cid: p02' / >".true);
helper.addInline("p01".new FileSystemResource(new File("C:\Users\hasee\Desktop\9cae14e699762b40a747d4198.jpg")));
helper.addInline("p02".new FileSystemResource(new File("C:\Users\hasee\Desktop\ wechat images_202101.jpg")));
javaMailSender.send(mimeMessage);
}
Copy the code
The test result is sent successfully