E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine.
Architectural engineering
Create a SpringBoot project and add it to its POM file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>Copy the code
Add the configuration
spring.mail.host=smtp.163.com
[email protected]
spring.mail.password=
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8Copy the code
Enter your email password in password.
Test email
The test code list is as follows:
package com.forezp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {
@Test
public void contextLoads() { } @Autowired private JavaMailSenderImpl mailSender; /** * send mail containing simple text */ @test public voidsendTxtMail() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); Simplemailmessage.setto (new String[] {"[email protected]"});
simpleMailMessage.setFrom("[email protected]");
simpleMailMessage.setSubject("Spring Boot Mail");
simpleMailMessage.setText("Here is a simple text."); // Send the mail mailsender. send(simpleMailMessage); System.out.println("Mail sent"); } /** * Send an email containing HTML text * @throws Exception */ @test public void sendHtmlMail() throws Exception {MimeMessage MimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); mimeMessageHelper.setTo("[email protected]");
mimeMessageHelper.setFrom("[email protected]");
mimeMessageHelper.setSubject("Spring Boot Mail [HTML]");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append(" Spring mail test
Hello! This is spring Mail test.
");
sb.append("</html>"); / / enable HTML mimeMessageHelper. SetText (sb. ToString (),true); // Send the mail mailsender. send(mimeMessage); System.out.println("Mail sent"); } /** * Send an email containing embedded images * @throws Exception */ @test public void sendAttachedImageMail() throws Exception {MimeMessage mimeMessage = mailSender.createMimeMessage(); // Multipart MimeMessageHelper MimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setTo("[email protected]");
mimeMessageHelper.setFrom("[email protected]");
mimeMessageHelper.setSubject("Spring Boot Mail");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append(" Spring mail test
Hello! This is spring Mail test.
"); // cid is fixed, imageId specifies an identifier sb.append("<img src=\"cid:imageId\"/></body>");
sb.append("</html>"); / / enable HTML mimeMessageHelper. SetText (sb. ToString (),true); ImageId FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
mimeMessageHelper.addInline("imageId", img); // Send the mail mailsender. send(mimeMessage); System.out.println("Mail sent"); } /** * Send mail containing attachments * @throws Exception */ @test public void sendAttendedFileMail() throws Exception {MimeMessage mimeMessage = mailSender.createMimeMessage(); // Multipart MimeMessageHelper MimeMessageHelper = new MimeMessageHelper(mimeMessage,true."utf-8");
mimeMessageHelper.setTo("[email protected]");
mimeMessageHelper.setFrom("[email protected]");
mimeMessageHelper.setSubject("Spring Boot Mail Mail test");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append(" Spring mail test
Hello! This is spring Mail test.
");
sb.append("</html>"); / / enable HTML mimeMessageHelper. SetText (sb. ToString (),true); FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
mimeMessageHelper.addAttachment("image.jpg", img); // Send the mail mailsender. send(mimeMessage); System.out.println("Mail sent"); }}Copy the code