Realize mail sending function under springboot

Springboot has encapsulated the mail function for us, which is very simple, and it only needs a little configuration to be ok.

import jar

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
View Code
MailService.java
@Service
public class MailService {

     @Autowired
     private JavaMailSender mailSender; //@Value("${spring.mail.username}") that comes with the framework //     
     Sender 's   mailbox such as [email protected] private String from;

  
   
     @Async   // Means to call this method asynchronously 
  public  void sendMail(String title, String url, String email) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from); // sender's mailbox 
    message.setSubject(title); // title 
    message.setTo(email); // to whom to send to the other party's mailbox 
    message.setText(url); // content 
    mailSender.send (message); // send 
  }

}

You also need to write the following configuration in the configuration file yml

spring.mail.host: smtp.163.com
spring.mail.username: 155156641xx@163.com
spring.mail.password: fill in your own
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true

it's ok



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324619625&siteId=291194637
Recommended