Spring boot发电子邮件

Posted 极限之旅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot发电子邮件相关的知识,希望对你有一定的参考价值。

废话不多说,写了个页面,根据用户输入的电子邮箱点击发送邮件按钮之后异步发送电子邮件,直接上代码

首先,在pom.xml需要引入spring-boot-starter-mail模块

pom.xml

<!--邮件模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户</title>
    <script th:src="@{/js/jquery.min.js}" type="text/javascript" charset="utf-8"></script>
    <!--要在js代码块中获得model的值,要使用内联th:inline-->
    <script th:inline="javascript">
        var contextPath = [[${#request.getContextPath()}]];
    </script>
    <script type="text/javascript">
        function sendMail() {
            var email=document.getElementById("email").value;
            // alert(email);
            $.ajax({
                url:contextPath+\'/user/send\',
                data:{\'email\':email},
                method:\'post\'
            })
        }
    </script>
</head>
<body>
    <div>
        <form>
            <label>用户名:</label>
            <input type="text" name="username">
            <br/>
            <label>邮箱号:</label>
            <input type="email" name="email" id="email">
            <br/>
            <input type="button" value="发送邮件" th:onclick="sendMail()"/>
        </form>
    </div>
</body>
</html>

IMailService.java

public interface IMailService {
    void sendMail(String email);
}

IMailServiceImpl.java

@Service
public class IMailServiceImpl implements IMailService {
    @Resource
    private JavaMailSender javaMailSender;

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

    @Override
    public void sendMail(String email) throws MailException{
        try {
            System.out.println(email);
            SimpleMailMessage message = new SimpleMailMessage();

            //邮件主题
            String title = "好看书城欢迎您";
            //邮件内容
            String text = "亲,恭喜您已成功注册为好看书城的会员,好看书城致力于为每一个爱看好书的会员提供完美的体验!您可以点击下面的链接:\\n  https://www.baidu.com  快速进入好看书城,在书海中自由的翱翔了。";
            //接收邮件人邮箱
            String to = email;

            // 发送人的邮箱(系统配置的邮箱)
            message.setFrom(from);
            //发给谁对方邮箱(接收有邮件人邮箱)
            message.setTo(to);
            //标题
            message.setSubject(title);
            //内容
            message.setText(text);
            //发件日期
            message.setSentDate(new Date());
            //发送
            javaMailSender.send(message);
            System.out.println("邮件已成功发送到:"+to);
        }catch (MailException e){
            e.printStackTrace();
            System.out.println("邮件发送失败!");
        }

    }
}

UserController.java

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IMailService iMailService;
    @RequestMapping("/send")
    public String send(String email){
//        System.out.println(email);
        iMailService.sendMail(email);
        return "index";
    }
}

application.yml

spring:
  mail:
    host: smtp.163.com
    port: 25
    username: wxb_0101@163.com
    password: 邮箱授权码
    default-encoding: UTF-8
server:
  port: 8888
  servlet:
    context-path: /mailsend

然后,访问localhost:8888/mailsend/ ,输入用户名和邮箱,点击发送邮件,就可以收到邮件了

以上是关于Spring boot发电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

1 分钟教会你用 Spring Boot 发邮件

从spring boot发邮件聊到开发的友好性

spring-boot 速成(10) -个人邮箱/企业邮箱发送邮件

Spring boot:thymeleaf 没有正确渲染片段

IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段