Chapter 19. SpringBoot: Mail Service

Author: Dream 1819
Original: https: //www.cnblogs.com/yanfei1819/p/11118340.html
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

introduction

  The importance of the message need not say, for example, registration verification, message notification, reminders, and other abnormalities, are inseparable from sending messages.


Version Information

  • JDK:1.8
  • SpringBoot :2.1.4.RELEASE
  • maven:3.3.9
  • IDEA:2019.1.1
  • mail:2.1.4.RELEASE


use

This example demonstrates that SpringBoot project to send mail.

First, create a project, dependent on the introduction of maven:

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

Then, the configuration information in application.properties in:

[email protected]
spring.mail.password=xxxxxx

spring.mail.host=smtp.xx.com
spring.mail.properties.mail.smtp.ssl.enable=true

Finally, test code:

package com.yanfei1819.mail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
    @Autowired
    private JavaMailSenderImpl sender;
    @Test
    public void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("开会");
        message.setText("今晚6点开会");
        message.setTo("[email protected]");
        message.setFrom("[email protected]");
        sender.send(message);
    }
}

Inbox can view the mapping results.

The above is send a simple message. How to send a complex message it can send the kind of attachment?

In fact, very simple, and the rest of the configuration are the same. To re-write a test method:

    @Test
    public void test() throws MessagingException {
        // 创建邮件
        MimeMessage message = sender.createMimeMessage();
        final MimeMessageHelper hepler = new MimeMessageHelper(message,true);
        // 邮件设置
        hepler.setSubject("开会");
        hepler.setText("<b>今晚6点开会</b>",true);
        hepler.setTo("[email protected]");
        hepler.setFrom("[email protected]");
        // 发送附件
        hepler.addAttachment("mybatis源码",new File("C:\\Users\\Administrator\\Desktop\\MyBatis源码分析.md"));
        // 邮件发送
        sender.send(message);
    }

Running, you can see the results.

Related Items

  SpringBoot mailing is not very simple? Developers only need to pay attention to several attributes and components need to be configured. Not much complicated code.

Let's look at the source code is how to achieve.

First find the spring-boot-autoconfigure-2.1.4.RELEASE.jar following mail package, carefully review MailSenderPropertiesConfigurationclasses,

    @Bean
    @ConditionalOnMissingBean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        applyProperties(sender);
        return sender;
    }

That is, the mail sending component. Readers can understand the principle of automatic configuration.

Configuration properties on the message, are MailPropertiesthe:

package org.springframework.boot.autoconfigure.mail;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {

    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
    /**
     * SMTP server host. For instance, `smtp.example.com`.
     */
    private String host;
    /**
     * SMTP server port.
     */
    private Integer port;
    /**
     * Login user of the SMTP server.
     */
    private String username;
    /**
     * Login password of the SMTP server.
     */
    private String password;
    /**
     * Protocol used by the SMTP server.
     */
    private String protocol = "smtp";
    /**
     * Default MimeMessage encoding.
     */
    private Charset defaultEncoding = DEFAULT_CHARSET;
    /**
     * Additional JavaMail Session properties.
     */
    private Map<String, String> properties = new HashMap<>();
    /**
     * Session JNDI name. When set, takes precedence over other Session settings.
     */
    private String jndiName;
    // set/get 省略
}



Guess you like

Origin www.cnblogs.com/yanfei1819/p/11118340.html