码迷,mamicode.com
首页 > 编程语言 > 详细

Spring boot发电子邮件

时间:2020-06-20 21:58:08      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:color   catch   ima   模块   页面   col   进入   charset   web   

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

首先,在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发电子邮件

标签:color   catch   ima   模块   页面   col   进入   charset   web   

原文地址:https://www.cnblogs.com/andrew3/p/13170310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!