邮件发送
- 邮件发送需要引入spring-boot-starter-mail
- Spring Boot自动配置MailSenderAutoConfiguration
- 定义MailProperties内容,配置在application.yml中
- 自动装配JavaMailSender ——用它来发送邮件
- 测试邮件发送
第一步、邮件发送需要引入spring-boot-starter-mail
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第二步、在配置文件,配置账号密码
#邮件任务
# 设置邮箱主机 如果是qq邮箱的话就是smtp.qq.com,如果是163邮箱的话就是smtp.163.com,下面username同样的
spring.mail.host= smtp.qq.com
spring.mail.port=465
#这里写自己的邮箱,密码的话不是真正的密码是授权码
spring.mail.username=xxxxxxxxxx@qq.com
//QQ邮箱开通第三方登录的授权码
spring.mail.password=xxxxxxxxxxx
spring.mail.default-encoding=UTF-8
//开启安全的连接
spring.mail.properties.mail.smtp.ssl.enable=true
#当SMTP需要SSL验证时,需要设定,如果不设定,会出现如下异常
spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
#spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
- 用SimpleMailMessage来封装发送简单邮件
- 用JavaMailSender这个接口注入的方式进行发送邮件,发送邮件的方法是send()
SpringBoot+Vue前后分离实现邮件发送功能
先看一下效果图
前端页面
邮件格式验证
发送成功提示
邮件收到的样式
email.class
package com.xiaofei.emaildemo.pojo;
import lombok.Data;
import java.util.Properties;
public class Email {
// 发送邮件的服务器的IP和端口
private static String mailServerHost;
private static String mailServerPort;
private static boolean isSSL;
// 邮件发送者的地址
private static String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private static String userName;
private static String password;
// 是否需要身份验证
private static boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
static{
mailServerHost = "smtp.163.com";
mailServerPort = "25";
isSSL = false;
validate = true;
//这里需要改成你的信息
userName = "javawebkaifa@163.com";
password = "IHRMJMKDENYMFASC";
fromAddress = "javawebkaifa@163.com";
}
/**
* 获得邮件会话属性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.stmp.timeout", "5000");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public boolean isValidate() {
return validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public String getPassword() {
return password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
public static boolean getIsSSL() {
return isSSL;
}
public static void setIsSSL(boolean isSSL) {
Email.isSSL = isSSL;
}
}
EmaiController.class
package com.xiaofei.emaildemo.controller;
import com.xiaofei.emaildemo.pojo.Email;
import com.xiaofei.emaildemo.util.EmailUtil;
import com.xiaofei.emaildemo.util.Result;
import com.xiaofei.emaildemo.util.StatusCode;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("email")
@CrossOrigin
public class EmaiController {
@RequestMapping
public Result tests(){
return new Result(true, StatusCode.OK,"成功!");
}
@RequestMapping(method = RequestMethod.POST)
public Result test(@RequestBody Map<String,String> map){
try{
Email mailInfo = new Email();
mailInfo.setToAddress(map.get("email"));
mailInfo.setSubject(map.get("title")); //邮件的title
mailInfo.setContent(map.get("content")); //邮件的内容
//这个类主要来发送邮件
EmailUtil.sendHtmlMail(mailInfo);
return new Result(true, StatusCode.OK,"发送成功!");
}catch (Exception e){
return new Result(false,StatusCode.ERROR,"发送失败!");
}
}
}
main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
import $ from 'jquery'
axios.defaults.baseURL = 'http://127.0.0.1:8080';
Vue.prototype.HOST='/email'
Vue.config.productionTip = false
Vue.prototype.$axios = axios
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
Helloworld.vue
<template>
<div>
<p style="padding: 30px 0px 10px; position: relative;"><span
class="u_logo"></span>
<input id="loginName" class="ipt" type="email" v-model="email" placeholder="请输入邮箱地址" value="">
<input class="ipt" id="password" type="text" v-model="title" placeholder="请输入邮件标题" value="">
</p>
<el-card style="height: 450px;">
<quill-editor v-model="content" ref="myQuillEditor" style="height: 500px;" :options="editorOption">
</quill-editor>
</el-card>
<div style="height: 50px; line-height: 50px; margin-top: -4px;background:#fff; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
<span style="float: right;">
<a id="loginBtn" @click="send()">发送</a>
</span></div>
</div>
</template>
<script>
import {
quillEditor
} from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
name: 'HelloWorld',
data () {
return {
email: "",
title:"",
content:"",
editorOption: {}
}
},
components: {
quillEditor
},
methods:{
send: function(){
//验证邮箱
var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
if(myReg.test(this.email)){
this.$axios.post("email", {
"email":this.email,
"title":this.title,
"content":this.content
}).then( res => {
alert(res.data.message)
}).catch( res => {
alert(res.data.message)
})
}else {
alert("邮箱格式不对!");
}
}
}
}
</script>
参考地址:
练习:
https://blog.csdn.net/ZZ2713634772/article/details/79576930
https://mp.weixin.qq.com/s/FpnWxRRlvKfdwVn_cB_DGw