“This is the 21st day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge 2021”.

In the development of Spring Boot projects, the function of sending emails is more or less exposed. If you want to realize sending emails in Spring projects, you need to encapsulate a relatively complex message body, but in Spring Boot projects, you only need to import third-party dependencies and configuration to complete.

This article takes mailbox 163 as an example to demonstrate the realization of email sending function. The configurations of other mailboxes are similar with minor differences.

1. Obtain the authorization code of the mailbox

Common mail transfer protocols include POP3, SMTP, and IMAP. SMTP is selected here for demonstration purposes.

First we log in to our email and find the protocol address in the Settings:

Then click open service to obtain the authorization code (the authorization code is displayed only once and must be kept well) :

The following are the three protocol host addresses corresponding to NetEase mailbox:

2. Introduce dependencies

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

3. Configure the mailbox information

⭐ Note: The following password refers to the authorization code, not the personal email password.

spring:
  mail:
    default-encoding: utf-8
    # host address
    host: smtp.163.com
    # mailbox name
    username: [email protected]
    # authorization code (not email password)
    password: xxxxxxxxxxxxxxx
Copy the code

4. Send emails

4.1 Plain text email

The following encapsulates the SimpleMailMessage message content, injecting the JavaMailSender and calling its Send () to send the message. Among them, recipients and cc recipients can be sent at the same time, used between multiple addresses, and can be splicing:

@RestController
public class EmailController {

    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("/sendMail")
    public void send(a) {
        SimpleMailMessage message = new SimpleMailMessage();
        / / the sender
        message.setFrom("[email protected]");
        // Recipient (batch sending can be realized)
        message.setTo("[email protected]"."[email protected]");
        // The header of the mailbox
        message.setSubject("Java sending mailbox Demo");
        // The contents of the email
        message.setText("Testing the Spring Boot integrated mailbox sending feature!");
        / / cc
        message.setCc("xxx? @163.com");
        // Send an emailjavaMailSender.send(message); }}Copy the code

4.2 Email in HTML format ⭐

Unlike sending plain text, sending HTML requires creating a MIME message and injecting it into the MimeMessageHelper object, but still sends it as the JavaMailSender interface:

@RestController
public class EmailController {

    @RequestMapping("/sendHtmlMail")
    public void sendHtmlMsg(a) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeHelper = new MimeMessageHelper(message, true);
        / / the sender
        mimeHelper.setFrom("[email protected]");
        / / the recipient
        mimeHelper.setTo("[email protected]");
        // The header of the mailbox
        mimeHelper.setSubject("Java sending mailbox Demo");
        // The contents of the email
        mimeHelper.setText("< h2 > < / h2 >" MimeMessageHelper test.true);

        // Still use the JavaMailSender interface to send messagesjavaMailSender.send(message); }}Copy the code

4.3 Mail with attachments ⭐

Only much more for addattroar () method compared to sending HTML:

@RestController
public class EmailController {

    @Autowired
    private JavaMailSender javaMailSender;
    
	@RequestMapping("/addAttachment")
    public void sendMailWithAttachment(a) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeWithAttachment = new MimeMessageHelper(message, true);
        / / the sender
        mimeWithAttachment.setFrom("[email protected]");
        / / the recipient
        mimeWithAttachment.setTo("[email protected]");
        // The header of the mailbox
        mimeWithAttachment.setSubject("Send mail (with attachments)");
        // The contents of the email
        mimeWithAttachment.setText("Add attachments...");

        // 1 attachment add image
        mimeWithAttachment.addAttachment("1.jpg".new File("D:\\Temp\\_mail\\1.jpg"));
        // 2. Attach a Word document
        mimeWithAttachment.addAttachment("test.docx".new File("D:\\Temp\\_mail\\test.docx"));

        // Still use the JavaMailSender interface to send messagesjavaMailSender.send(message); }}Copy the code

4.4 Embedded picture mail

AddInline () sends an image:

@RestController
public class EmailController {

    @Autowired
    private JavaMailSender javaMailSender;
    
    @RequestMapping("/embeddedPic")
    public void mailWithEmbeddedPic(a) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper embeddedPic = new MimeMessageHelper(message, true);
        / / the sender
        embeddedPic.setFrom("[email protected]");
        / / the recipient
        embeddedPic.setTo("[email protected]");
        // The header of the mailbox
        embeddedPic.setSubject("Send a message (embedded image)");
        // The contents of the email
        embeddedPic.setText("Send embedded picture..");
        // Add an inline image
        embeddedPic.addInline("img1.jpg".new FileSystemResource("D:\\Temp\\_mail\\1.jpg"));

        // Still use the JavaMailSender interface to send messagesjavaMailSender.send(message); }}Copy the code

At this point, we have quickly implemented mail sending in the Spring Boot environment. Is it easier and faster than expected? !

We hope this article will help you 🧠 feel free to leave your thoughts in the comments 🌊, we will discuss and share 🔥