springboot simple mail send

Keywords: Java Spring encoding Thymeleaf xml

Reasons for writing:

Near the end of the project, the requirements change again and again. In fact, the technical point has never changed. It's just the change of business logic and the function of sending e-mail reminders. It's changed seven times in two months. I want to record the technical points. It's not about the business here, it's only about the function of sending email.

Mail preparation instructions:

Due to the project requirements of the company, the mailbox we use is the internal mailbox of the company, so the mailbox provided by the Department to our system is the international mailbox, that is, the mailbox with the highest authority. Therefore, mailbox configuration needs to be configured according to the form of mailbox provided.

pom.xml

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

application.yml

spring:
  mail:
    host: smtp.xxx.com
    port: 25
    username: xxx@xxx.com
    password: xxx
    default-encoding: UTF-8
  thymeleaf: false

Note: host: the default attribute is the host of the JavaMail session;

    Port: port 25 is the default listening standard SMTP port. If it is disabled internally, 465 can be used;
    username: sender's mailbox;
    Password: email password. If it is qq or 163 email, it is the email authorization code;
    Default encoding: message encoding character set;
    Thmeleaf: template generates email. The solution given by Spring is to use template to generate email. There are many template schemes to choose from;

controller :

import io.swagger.annotations.Api;
​
​
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("mail")
@Api(description = "Mail delivery")
public class MailSendController {
​
    @Autowired
    private IMailService service;
​
    @PostMapping("/sendText")
    public ResponseStatus send() {
        return service.sendMail();
    }
}

service:

import xxx.xxx.xxx.xxx.ResponseStatus;
​
​
public interface IMailService {
​
    public void sendMail();
}

IMailServiceImpl:

import xxx.xxx.xxx.xxx.IMailService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service
public class IMailServiceImpl implements IMailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;


    @Override
    public void sendMail(String title, String text, String to) {
        //Mail object
        SimpleMailMessage message = new SimpleMailMessage();
        
        //Mail theme
        String title = "I'm the subject of the test email";
        //Mail content
        String text = "I am the content of this test email. It has a lot of content and can be filled in according to my own business";
        //Email to
        String to = "xxx@xxx";
        
        
        // Sender's mailbox (system configured mailbox)
        message.setFrom(from); 
        //To whom (to whom)
        message.setTo(to);
        //Title
        message.setSubject(title); 
        //content
        message.setText(text); 
        //Date of issue
        message.setSentDate(new Date());
        //Send out
        javaMailSender.send(message); 
        ResponseStatus.ok(); 
    }
}

Conclusion:

 1. Special attention should be paid to the mailbox configuration in the yum file. When configuring password for different mailboxes, special attention should be paid. If the enterprise's own mailbox is not set, it is the mailbox password, which I use. QQ mailbox and 163 mailbox are mailbox authorization codes here. How to obtain the authorization code can ask "Du Niang" by yourself. 

2. The default setting of mail port is 25. If the port is occupied, use 465.
3. Spring boot integrates email service records. As Xiaobai grows up, I hope you can give me some advice.

Author:Soul Hau Xuan Welcome to the public address.

I reserve all rights and interests. Please indicate the source of reprint.
Welcome friends with stories and ideas to share with me, which can be sent to e-mail: lwqforit@163.com

Posted by GetPutDelete on Thu, 05 Dec 2019 09:49:23 -0800