springboot send mail (1): send simple mail

Springboot implements the mail sending function:

1. Overall directory structure:


2. Create a new springboot project: add dependencies in pom.xml:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
</parent>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>


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



3. Write the application.properties configuration file in the resources file directory as follows:

spring.application.name = spirng-boot-mail
 spring.mail.host = smtp.qq.com
 [email protected] spring.mail.password
 = authorization code
 spring.mail.default - encoding = UTF -8
 mail.fromMail.addr = [email protected] //Indicate the sender (you can not write, personal preference)



//If the following 3 sentences are not added, a 530 error will be reported
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
Remember to open the pop3/smtp service in the qq mailbox, Settings- >Account->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service and obtain the authorization code.


4. Write the sending mail service interface to realize the sending mail function:

/**
 * 邮件服务接口
 * Created by ASUS on 2018/5/5
 *
 * @Authod Grey Wolf
 */
public interface MailService {

    /**
     * 发送简单邮件
     * @param to
     * @param subject
     * @param content
     */
    void sendMail(String to,String subject,String content);
}
/**
 *
 * 邮件服务类
 * Created by ASUS on 2018/5/5
 *
 * @Authod Grey Wolf
 */

@Service("mailService")
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender mailSender;
    
//读取application.properties的内容
  @Value("${mail.fromMail.addr}")
    private String form;
    /**
     * 发送简单邮件
     * @param to 接受者
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(form);//发起者
        mailMessage.setTo(to);//接受者

    //如果发给多个人的:
    //mailMessage.setTo("1xx.com","2xx.com","3xx.com");     
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            mailSender.send(mailMessage);
            System.out.println("发送简单邮件");
        }catch (Exception e){
            System.out.println("发送简单邮件失败");
        }
    }
}

5.编写邮件测试类:

/**
 * 发送邮件测试类
 * Created by ASUS on 2018/5/5
 *
 * @Authod Grey Wolf
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {

    @Autowired
    private MailService mailService;

    @Value("${mail.fromMail.addr}")
    private String form;

    @Test
    public  void sendSimpleMail() throws Exception{
        mailService.sendMail(form,"简单邮件","springboot实现邮件发送");
    }
}

6.记得在根目录下编写个springboot的启动类,如果没写的,会报错哦。

/**
 * 启动类
 */
@SpringBootApplication
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }

}

7.run测试类MailTest:,查看结果:



我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。

Guess you like

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