maven配置
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml配置
## QQ邮箱配置
spring:
mail:
host: smtp.qq.com #发送邮件服务器
username: 1649346712@qq.com #发送邮件的邮箱地址
password: *****#客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
properties.mail.smtp.port: 465 #端口号465或587
from: 1649346712@qq.com # 发送邮件的地址,和上面username一致
#可以任意
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
邮件配置工具类
package com.gecko.charging.util;
import com.meenoframework.util.MeenoAssert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @description: 邮件工具类
* @author: Wzq
* @create: 2020-03-03 17:04
*/
@Component
public class EmailUtils {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用
*/
@Autowired
private JavaMailSender mailSender;
/**
* 配置文件中我的qq邮箱
*/
@Value("${spring.mail.from}")
private String from;
/**
* 简单文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Async
public void sendSimpleMail(String to, String subject, String content) {
//创建SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人
message.setFrom(from);
//邮件接收人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
mailSender.send(message);
}
/**
* html邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Async
public void sendHtmlMail(String to, String subject, String content) {
//获取MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(message, true);
//邮件发送人
messageHelper.setFrom(from);
//邮件接收人
messageHelper.setTo(subject);
//邮件主题
message.setSubject(subject);
//邮件内容,html格式
messageHelper.setText(content, true);
//发送
mailSender.send(message);
//日志信息
logger.info("邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常!", e);
MeenoAssert.isTrue(false,"发送邮件时发生异常!");
}
}
/**
* 带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
@Async
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
//日志信息
logger.info("邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常!", e);
MeenoAssert.isTrue(false,"发送邮件时发生异常!");
}
}
}
调用发送邮件
package com.gecko.charging.partner.controller.agent;
import com.gecko.charging.partner.entity.Partner;
import com.gecko.charging.partner.enums.PartnerType;
import com.gecko.charging.partner.model.PartnerModel;
import com.gecko.charging.partner.model.PartnerPageSearchCondModel;
import com.gecko.charging.partner.service.PartnerService;
import com.gecko.charging.partner.view.PartnerManagerView;
import com.gecko.charging.util.EmailUtils;
import com.meeno.boot.uim.common.ResultUtil;
import com.meenoframework.util.PageData;
import com.meenoframework.util.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description: 代理商Controller
* @author: Wzq
* @create: 2020-03-02 14:20
*/
@RestController
@RequestMapping("/partner/agentManager")
@Slf4j
public class AgentManagerController {
@Autowired
EmailUtils emailUtils;
/**
* 发送邮件
* @return
*/
@RequestMapping("sendEmail.action")
public ResponseBean sendEmail(){
String content = "\n" +
"尊敬的李XX:\n" +
" 您的代理商账号已开通,请妥善保留好自己的账号密码。登录***平台查看及管理。\n" +
" 账号:AAAAAAAAAA\n" +
" 密码:123456\n" +
" 网址: www.baidu.com\n" ;
emailUtils.sendSimpleMail("2428465028@qq.com", "【壁虎充电】账号开通成功提醒",content);
return ResultUtil.success();
}
}
成功!
image.png关注公众号回复“书籍”就可以看大佬看得同款书籍了:
image.png
网友评论