1.先配置自己的邮箱,获取校验码,将自己的邮箱配置成客户端
查看:
2.配置application.yml文件
spring:
mail:
#发送邮件服务器
host: smtp.qq.com
#邮箱用户名
username: @qq.com
#邮箱授权码
password: ***********
#字符编码
default-encoding: utf-8
3.创建一个实体类,接受前台传参数据
package com.ds.entity;
import java.io.Serializable;
public class Mail implements Serializable {
private String mail;//邮箱账号
private String topic;//主题
private String message;//内容或者html页面内容
private String fileid;//fileid
public Mail() {
}
public Mail(String mail, String topic, String message, String fileid) {
this.mail = mail;
this.topic = topic;
this.message = message;
this.fileid = fileid;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getFileid() {
return fileid;
}
public void setFileid(String fileid) {
this.fileid = fileid;
}
}
4.创建service接口
package com.ds.service;
public interface MailService {
/**
* 发送文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
void sendSimpleMail(String to, String subject, String content);
/**
* 发送HTML邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath);
}
5.创建serviceimpl实现类
package com.ds.service.impl;
import com.ds.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender javaMailSender;
//注入配置文件中配置的信息——>from
@Value("${spring.mail.username}")
private String from;
/**
* 发送文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
log.info("发送文本邮件---->");
log.info("发送邮件至:"+to);
log.info("发送邮件主题:"+subject);
log.info("发送文本内容:"+content);
SimpleMailMessage message = new SimpleMailMessage();
//发件人
message.setFrom(from);
//收件人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
javaMailSender.send(message);
}
/**
* 发送HTML邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
log.info("发送HTML邮件---->");
log.info("发送邮件至:"+to);
log.info("发送邮件主题:"+subject);
log.info("发送文本内容:"+content);
messageHelper = new MimeMessageHelper(message,true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
message.setSubject(subject);
messageHelper.setText(content,true);
javaMailSender.send(message);
log.info("邮件已经发送!");
} catch (MessagingException e) {
log.error("发送邮件时发生异常:"+e);
}
}
/**
* 发送带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
log.info("发送带附件邮件---->");
log.info("发送邮件至:"+to);
log.info("发送邮件主题:"+subject);
log.info("发送文本内容:"+content);
messageHelper = new MimeMessageHelper(message,true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content,true);
//携带附件
FileSystemResource file = new FileSystemResource(filePath);
if(file.exists()){//如果文件存在
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
messageHelper.addAttachment(fileName,file);
}
javaMailSender.send(message);
log.info("邮件加附件发送成功!");
} catch (MessagingException e) {
log.error("发送失败:"+e);
}
}
}
6.创建接口调用
package com.ds.controller;
import com.ds.common.FilePath;
import com.ds.entity.File;
import com.ds.entity.Mail;
import com.ds.entity.RespBean;
import com.ds.service.FileService;
import com.ds.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
@Autowired
private JavaMailSender javaMailSender;
@Autowired
MailService mailService;
@Autowired
FileService fileService;
@RequestMapping("/send")
public String sendMail(){//测试用例
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("@qq.com");//发送人邮箱
message.setTo("@qq.com");//收件人邮箱
message.setSubject("11111111");//发送标题
message.setText("笑笑笑");//发送内容
try {
javaMailSender.send(message);
return "成功";
}catch (Exception e){
e.printStackTrace();
return "失败";
}
}
@RequestMapping("/sendSimpleMail")
public RespBean sendSimpleMail(Mail mail){
try {
mailService.sendSimpleMail(mail.getMail(),mail.getTopic(),mail.getMessage());
return RespBean.sucess("发送成功!");
}catch (Exception e){
e.printStackTrace();
return RespBean.error("发送失败!");
}
}
@RequestMapping("/sendHtmlMail")
public RespBean sendHtmlMail(Mail mail){
try {
mailService.sendHtmlMail(mail.getMail(),mail.getTopic(),mail.getMessage());
return RespBean.sucess("发送成功!");
}catch (Exception e){
e.printStackTrace();
return RespBean.error("发送失败!");
}
}
@RequestMapping("/sendFileMail")
public RespBean sendFileMail(Mail mail){
try {
File file = fileService.getFileByid(mail.getFileid());
if(file!=null){
mailService.sendAttachmentsMail(mail.getMail(),mail.getTopic(),mail.getMessage(), FilePath.PATH+file.getPath());
}else{
mailService.sendAttachmentsMail(mail.getMail(),mail.getTopic(),mail.getMessage(),null);
}
return RespBean.sucess("发送成功!");
}catch (Exception e){
e.printStackTrace();
return RespBean.error("发送失败!");
}
}
}
其中:RespBean是我自定义的返回实体类。
package com.ds.entity;
/*
* 公共返回对象
* */
public class RespBean {
private long code;
private String message;
private Object obj;
public RespBean() {
}
public RespBean(long code, String message, Object obj) {
this.code = code;
this.message = message;
this.obj = obj;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
@Override
public String toString() {
return "RespBean{" +
"code=" + code +
", message='" + message + '\'' +
", obj=" + obj +
'}';
}
/*
* 成功返回结果
* */
public static RespBean sucess(String message){
return new RespBean(200,message,null);
}
/*
* 成功返回结果
* */
public static RespBean sucess(String message,Object obj){
return new RespBean(200,message,obj);
}
/*
* 失败返回结果
* */
public static RespBean error(String message){
return new RespBean(500,message,null);
}
/*
* 失败返回结果
* */
public static RespBean error(String message,Object obj){
return new RespBean(500,message,obj);
}
}