admin管理员组

文章数量:1634823

点击链接查看项目

SpringBoot邮件发送

邮件发送是一个非常常见的功能,注册时的身份认证、重要通知发送等都会用到邮件发送。Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐,Spring 中提供了JavaMailSender 用来简化邮件配置,Spring Boot则提供了MailSenderAutoConfiguration 对邮件的发送做了进一步简化。

发送前的准备

使用QQ邮箱发送邮件,首先要申请开通POP3/SMTP服务或者IMAP/SMTP服务。SMTP全称为Simple Mail Transfer Protocol,译作简单邮件传输协议,它定义了邮件客户端软件与SMTP服务器之间,以及SMTP服务器与SMTP服务器之间的通信规则。也就是说,aaa@qq 用户先将邮件投递到腾讯的SMTP服务器,这个过程就使用了SMTP协议,然后腾讯的SMTP服务器将邮件投递到网易的SMTP服务器,这个过程依然使用了SMTP协议,SMTP服务器就是用来接收邮件的。而POP3全称为Post Office Protocol3,译作邮局协议,它定义了邮件客户端与POP3服务器之间的通信规则。该协议在什么场景下会用到呢?当邮件到达网易的SMTP服务器之后,111@163 用户需要登录服务器查看邮件,这个时候就用上该协议了:邮件服务商会为每一个用户提供专门的邮件存储空间,SMTP服务器收到邮件之后,将邮件保存到相应用户的邮件存储空间中,如果用户要读取邮件,就需要通过邮件服务商的POP3邮件服务器来完成。至于IMAP协议,则是对POP3协议的扩展,功能更强,作用类似。

后端java代码

UserController控制层代码:

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.rest.CodeMsg;
import com.example.demo.rest.Result;
import com.example.demo.service.UserRegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin
public class UserController {
   
    @Autowired
    private UserRegistService userRegistService;

    @PostMapping("/sendCode")
    public Object sendCode(@Valid User vo) {
   
        try {
   
            return userRegistService.sendCode(vo);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @PostMapping("/regist")
    public Object startRegist(@RequestParam("username") String username , @RequestParam("code") String code) {
   
        try {
   
           return userRegistService.regist(username,code);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @PostMapping("/updateCode")
    public Object active(@RequestParam("code") String code) {
   
        try {
   
            return userRegistService.update(code);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @PostMapping("/active")
    public Object login(@RequestParam("username") String username, @RequestParam("pwd") String pwd) {
   

        try {
   
            return userRegistService.login(username, pwd);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }
}

UserDao数据连接层代码:

package com.example.demo.dao;

import com.example.demo.entity.ExamScore;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface UserDao {
   
    void insert(User vo);

    void update(String code);

    User findByUserName(String userName);

    User findByNameAndPwd(Map<String, String> map);

    void deleteByUserName(String userName);

}

UserEntity实体层代码:

package com.example.demo.entity;


import java.util.Date;

public class User {
   
    private Integer id;
    private String username;
    private String pwd;
    private Integer jh;
    private Integer scores;
    private String email;
    private String code;
    private Date createdate;

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public String getUsername() {
   
        return username;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public String getPwd() {
   
        return pwd;
    }

    public void setPwd(String pwd) {
   
        this.pwd = pwd;
    }

    public Integer getScores() {
   
        return scores;
    }

    public void setScores(Integer scores) {
   
        this.scores = scores;
    }

    public String getEmail() {
   
        return email;
    }

    public void setEmail(String email) {
   
        this.email = email;
    }

    public String getCode() {
   
        return code;
    }

    public void setCode(String code) {
   
        this.code = code;
    }

    public Date getCreatedate() {
   
        return createdate;
    }

    public Integer getJh() {
   
        return jh;
    }

    public void setJh(Integer jh) {
   
        this.jh = jh;
    }
    public void setCreatedate(Date createdate) {
   
        this.createdate = createdate;
    }
    @Override
    public String toString() {
   
        return "User{" +
                ", username='" + username + '\'' +
                ", pwd='" + pwd + '\'' +
                ", jh=" + jh +
                ", scores=" + scores +
                ", email='" + email + '\'' +
                ", code='" + code + '\'' +
                ", createdate=" + createdate +
                '}';
    }
}

注册操作信息提示以及注册成功或失败时调用函数:

package com.example.demo.rest;

/**
 * @description 错误信息
 * @author dcl
 * @date 2019/12/17
 *
 */
public class CodeMsg {
   
	
	private int code;
	private String msg;
	
	public static final CodeMsg SUCCESS = new CodeMsg(0, "操作成功");
	
	public static final CodeMsg REGIST_SUCCESS = new CodeMsg(0, "注册成功,点击确定前往登录界面登录");
	
	public static final CodeMsg ACTIVE_SUCCESS = new CodeMsg(0, "用户激活成功");

	public static final CodeMsg REGIST_FALSE = new CodeMsg(-1, "注册失败,验证码错误,请重新输入");

	public static final CodeMsg USEREXISTS = new CodeMsg(500101,"用户已经存在");
	
	public static final CodeMsg USENOTREXISTS = new CodeMsg(500102,"账号或密码错误");
	
	public static final CodeMsg SERVER_ERROR = new CodeMsg(500100,"服务端异常");
	
	//临期管理模块异常
	public CodeMsg(int code,String msg)
	{
   
		this.code = code;
		this.msg = msg;
	}
	
	public int getCode() {
   
		return code;
	}
	public String getMsg() {
   
		return msg;
	}
	
	
}
package com.example.demo.rest;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;


public class Result<T> 
{
   
	private int code;
	private String msg;
	@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
	private T data;
	
	/**
	 * 返回成功时调用
	 * @param data
	 * @return
	 */
	public static <T> Result<T> success(T data)
	{
   
		return new Result<T>(data);
	}
	
	/**
	 * 返回失败时调用
	 * @param data
	 * @return
	 */
	public static <T> Result<T> error(CodeMsg cm)
	{
   
		return new Result<T>(cm);
	}
	
	private Result(T data){
   
		this.code = 0;
		this.msg = "操作成功";
		this.data = data;
	}
	
	private Result(CodeMsg cm){
   
		if(cm == null)
		{
   
			return;
		}
		this.code = cm.getCode();
		this.msg = cm.getMsg();
	}
	public int getCode() {
   
		return code;
	}
	public void setCode(int code) {
   
		this.code = code;
	}
	public String getMsg() {
   
		return msg;
	}
	public void setMsg(String msg) {
   
		this.msg = msg;
	}
	public T getData() {
   
		return data;
	}
	public void setData(T data) {
   
		this.data = data;
	}
}

service服务接口层:

本文标签: 邮箱功能SpringBootvue