Home > Java > javaTutorial > body text

How to implement the email sending function in springboot

PHPz
Release: 2023-05-13 10:10:06
forward
1506 people have browsed it

Sending emails is a very common function, and its implementation in Java requires the JavaMailSender interface. In the springboot project, a dependency named spring-boot-starter-mail needs to be introduced. If there are requirements for the format of the email, you can introduce a spring-boot-starter-thymeleaf dependency that can operate html files.

 <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>
Copy after login

Like other automated configuration modules, after completing the dependency introduction, you need to configure the corresponding property values ​​in application.properties, otherwise the running method will always report a null pointer.

1. Create a new springboot project.

1. Open idea, click the file button on the toolbar in the upper left corner, and create a new boot project

How to implement the email sending function in springboot

2. Click next and select the default dependencies, common There is a database connection, web, etc.

How to implement the email sending function in springboot

3. Click finsh and wait for the boot project directory to be generated. The directory at this time is incomplete and you need to add the java and resources folders yourself. Right-click the project and select

How to implement the email sending function in springboot

to add it.

How to implement the email sending function in springboot

4. Then open the pom file and introduce dependencies.

5. Open the configuration file and write the corresponding parameters.

How to implement the email sending function in springboot

Once the project is completed, start writing test classes.

2. Send emails

1. Create a util class and write a business class that implements sending logic. There is nothing to write a tool class because I want to implement a format when sending emails. Emails with attachments, HTML styles, and asynchronous operations. Everyone knows that sending emails is time-consuming, especially asynchronous.

How to implement the email sending function in springboot

2. Add a way to write an asynchronous call:

*Add @Async if you need asynchronous methods
*Need to enable asynchronous on the startup class Method, @EnableAsync
*Note that it may be because of the aop proxy. If the called method and the calling code are in the same class, it is just equivalent to this class call, and no proxy class is used, so @Async It has no effect, it is in a tool class.

3. Code: Email with attachment

 @Test
    public void sendAttachmentsMail() {
        Context context = new Context();
        context.setVariable("agencyName", "11");
        context.setVariable("busTypeName", "22");
        context.setVariable("busAllowance", 33);
        String emailContent = templateEngine.process("emailTeplate", context);
 
        try {
            emailService.sendAttachmentsMail(new String[]{"xxx.com"}, "测试提示", emailContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("send mail success!,please wait a few mintens");
    }
Copy after login
/**
     * fujian
     * @throws Exception
     */
    @Async
    public void sendAttachmentsMail(String[] to, String subject, String contnet) throws Exception {
 
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("xxx.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(contnet,true);
        Path picturePath = Paths.get("E:WorkFiles	estBill", "test.png");
        byte[] bytes = Files.readAllBytes(picturePath);
        helper.addAttachment("附件-1.jpg", picturePath.toFile());
 
        mailSender.send(mimeMessage);
 
    }
Copy after login

Email sent successfully

How to implement the email sending function in springboot

The above is the detailed content of How to implement the email sending function in springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template