Springboot--JavaMail to send mail

Hello everyone! The knowledge I will share with you today is that Springboot integrates JavaMail to send emails.

1. What is JavaMail?

JavaMail, as the name suggests, provides developers with programming interfaces related to email processing  . It is an API released by Sun to handle email. It can easily perform some common mail transfers. We can develop applications similar to Microsoft Outlook based on JavaMail. The simple understanding is that you can use it to send emails using java.

2. Configure and run

1.Configuration

It’s still the same as before, first build the project, and then import the JavaMail dependencies.

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

After importing dependencies, you need to configure the yml file.

 

There are several points here that are required . The first host means what email address we use to send, with the smtp protocol in front. The second username is the email address we use to send emails, and the third password is also required. This password is not written randomly. I am using QQ mailbox here, and I need to get this password from QQ mailbox. Let me tell you below.

First open the QQ mailbox, click Settings

 

Then click Account and slide down. 

 

Then find the SMTP service, click to open it, and a password of your own will appear. We can just copy the password that appears. (Since I have already enabled it, I won’t demonstrate it anymore)

 This completes the previous configuration work.

2.Send email

Let's first build a Service layer and an implementation class.

First write the interface, the code is as follows:

package com.example.springboot_javaemail.service;


public interface SendMailService {
    void SendMail();
}

Implementation class, the code is as follows:

package com.example.springboot_javaemail.service.impl;

import com.example.springboot_javaemail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender; //javamail对象
    //发送人
    private String from = "[email protected]";
    //接收人
    private String to ="[email protected]";
    //标题
    private String subject="测试邮件";
    //正文
    private String context="测试!!!你好!我是亚不帅";


    @Override
    public void SendMail() {
        SimpleMailMessage message = new SimpleMailMessage(); //信息
        message.setFrom(from+"(亚不帅)"); //发送人
        message.setTo(to); //接收人
        message.setSubject(subject); //标题
        message.setText(context); //内容
        //message.setSentDate(); //定时发送
        javaMailSender.send(message); //发送
    }
}

Here you need to create another SimpleMailMessage object. You must know that SimpleMailMessage is a value object, which encapsulates some simple attributes, such as from, to, subject, text, etc. SimpleMailMessage can only be used to send emails in text format.

 Then let's test it

package com.example.springboot_javaemail;

import com.example.springboot_javaemail.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootJavaemailApplicationTests {

    @Autowired
    private SendMailService service;

    @Test
    void contextLoads() {
        service.SendMail();
    }

}

 

As you can see, the email was sent successfully. 


 

Summarize

This article only talks about sending simple text emails. It is relatively simple to learn. Please point out any shortcomings! Thank you for watching! !

Guess you like

Origin blog.csdn.net/m0_66403070/article/details/130717524