preface

The following steps are used to implement the email sending function

Introducing rely on spring – the boot – start E-mail with SpringBoot automatic configuration MailSenderAutoConfiguration definition MailProperties content, Configure the automatic assembly of JavaMailSender to test mail sending in application.yml (also available with application.properties)

Import dependence

A new SpringBoot project imports the following dependencies in the POM.xml file

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId>  </dependency>MaiCopy the code

The configuration file

Import dependence need to configure a called MailSenderAutoConfiguration after class, you can configure the parameters have the port (port), the host (host name), the username (SMTP user, SMTP is a protocol that provides reliable and efficient email transfer), password can be configured in the configuration file application.properties under the Resources directory

# Use QQ email, the suffix is @qq.com, if it is 163, it is @163.com[email protected] spring. Mail. Password = slightly# If it is qq email, it is SMTP, other email only need to change the suffix qq.com
spring.mail.host=smtp.qq.com
# QQ need to configure SSL, enable encryption authentication, if other email does not need
spring.mail.properties.mail.smtp.ssl.enable=true
Copy the code

You need to obtain the authorization code instead of directly entering the email password to avoid exposing your email password

Obtaining the authorization code Take QQ as an example. Enter the QQ mailbox and enter the account page under the Settings

Then slide down to the service

Fill in the authorization code in spring.mail.password and the configuration is almost complete

Write test code

Start writing test classes. Write test classes that send simple messages

@ SpringBootTest classSpringbootTaskApplicationTests {@autowired / / automatic assembly JavaMailSenderImpl mailSender; // The object that implements mail sending @testpublicvoidcontextLoad(){// Mail setting 1: SimpleMailMessage =new SimpleMailMessage(); message.setSubject("Notice. - Meeting tomorrow."); // Set the title message.settext ("Meeting tonight at 7:30."); // Fill in the text content message.setto ("[email protected]"); // The receiver can set up its own mailbox to test message.setFrom("[email protected]"); // The sender's mailbox mailsender.send (message); // send}}Copy the code

Set the email sender and recipient as you, right-click the run test, and find that the file is sent successfully. You receive the email

Send complicated emails

	@Test
publicvoidcontextLoad2()throwsMessagingException{
	MimeMessage     mimeMessage=mailSender.createMimeMessage();
	MimeMessageHelper  mimeMessageHelper= new   MimeMessageHelper(mimeMessage,true); . / / MimeMessageHelper can set send attachments MimeMessageHelper setSubject ("Notice of meeting tomorrow.");
	mimeMessageHelper.setText(< p style='color:blue'>.true); // Set it heretrueCan read HTML language, set style for text, also can not fill / / send attachments, here is the attached two pictures mimeMessageHelper. AddAttachment ("1.jpg",newFile("C: \ \ Users \ \ lenovo \ \ Desktop \ \ dumplings. JPG")); / / the front is to set up later, the name of the file is the absolute path to the file, can also be a relative path mimeMessageHelper. AddAttachment ("2.jpg",newFile("C: \ \ Users \ \ lenovo \ \ Desktop \ \ life. JPG"));
	mimeMessageHelper.setTo("[email protected]");
	mimeMessageHelper.setFrom("[email protected]");
	
	mailSender.send(mimeMessage);
}
Copy the code

Test as before, send successfully

The last

Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!