springboot邮件发送

SpringBoot邮件发送

1.新建SpringBoot项目

导入spring- boot -starter-mail依赖

pom.xml文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.4version>
        <relativePath/> 
    parent>
    <groupId>com.qfedugroupId>
    <artifactId>days73spt_mailartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>days73spt_mailname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

2.在resource下创建配置文件

application.yml

spring:
  mail:
    host: smtp.qq.com  注意配置不同邮箱的host
    username:[email protected] 发信方
    password: ncapuhlybmkdihej   #自行修改为自己的邮箱授权码客户端授权码
    default-encoding: utf-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

springboot邮件发送_第1张图片

3.service层

EmailService接口

package com.service;

public interface IEmailService {

    Integer sendMail(String mail);
}

接口实现类

package com.service.impl;

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

@Service
public class EmailServiceImpl implements IEmailService {

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

    @Autowired
    JavaMailSender sender;

    @Override
    public Integer sendMail(String mail) {
        SimpleMailMessage smm = new SimpleMailMessage();

        smm.setFrom(from);
        smm.setTo(mail);
        smm.setSubject("发送的标题");
        smm.setText("发送的内容");

        try {
            sender.send(smm);

            return 1;
        } catch (MailException e) {
            e.printStackTrace();
        }

        return 0;
    }
}

4.controller层

package com.controller;

import com.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailController {

    @Autowired
    private IEmailService ies;

    @GetMapping("/sendMail")
    public boolean sendMail(@RequestParam("mail") String mail){
        return ies.sendMail(mail) > 0;
    }
}

5.发送邮件

http://localhost:8080?mail=收信方的邮件

springboot邮件发送_第2张图片

你可能感兴趣的:(Javaweb学习笔记,spring,boot,spring,java)