1.业务代码
实体类(存放接口的发送信息)
public class MailVO { /** * 邮件Id */ @ApiModelProperty(value = "邮件Id", name = "邮件Id") private String id; /** * 邮件发送人 */ @ApiModelProperty(value = "邮件发送人", name = "邮件发送人") private String emailSender; /** * 邮件接收人(多个邮箱则用逗号","隔开) */ @ApiModelProperty(value = "邮件接收人", name = "邮件接收人") private String emailReciever; /** * 邮件主题 */ @ApiModelProperty(value = "邮件主题", name = "邮件主题") private String emailSubject; /** * 邮件内容 */ @ApiModelProperty(value = "邮件内容", name = "邮件内容") private String emailText; /** * 发送时间 */ @ApiModelProperty(value = "发送时间", name = "发送时间") private Long sentDate; /** * 抄送(多个邮箱则用逗号","隔开) */ @ApiModelProperty(value = "抄送(多个邮箱则用逗号\",\"隔开)", name = "抄送(多个邮箱则用逗号\",\"隔开)") private String carbonCopy; /** * 密送(多个邮箱则用逗号","隔开) */ @ApiModelProperty(value = "密送(多个邮箱则用逗号\",\"隔开)", name = "密送(多个邮箱则用逗号\",\"隔开)") private String blindCarbonCopy; /** * 状态 */ @ApiModelProperty(value = "状态", name = "状态") private String status; /** * 报错信息 */ @ApiModelProperty(value = "报错信息", name = "报错信息") private String error; /** * 邮件附件 */ @JsonIgnore @ApiModelProperty(value = "邮件附件", name = "邮件附件") private MultipartFile[] multipartFiles; }
controller
@RestController @RequestMapping("/mail") public class MailController { @Autowired private MailService mailService; /** * 功能描述: 发送邮件 * * @param mailVO * @return MailVO */ @PostMapping("/send") @ApiOperation(value = "发送邮件") public MailVO sendMail(@RequestBody MailVO mailVO) { //发送邮件和附件 return mailService.sendMail(mailVO); } }
service
public interface MailService { /** * 功能描述:发送邮件 * * @param mailVO * @return MailVO */ MailVO sendMail(MailVO mailVO); /** * 功能描述:检测邮件信息类 * * @param mailVo * @return */ void checkMail(MailVO mailVo); /** * 功能描述:构建复杂的邮件 * * @param mailVo * @return */ void sendMimeMail(MailVO mailVo); /** * 功能描述:保存邮件 * * @param mailVo * @return mailVo */ MailVO saveMail(MailVO mailVo); }
serviceimpl(发送邮件的逻辑功能)
@Service("mailService") public class MailServiceImpl implements MailService { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private JavaMailSenderImpl mailSender; @Override public MailVO sendMail(MailVO mailVO) { try { //1.检测邮件 checkMail(mailVO); //2.发送邮件 sendMimeMail(mailVO); //3.保存邮件 return saveMail(mailVO); } catch (Exception e) { //打印错误信息 logger.error("发送邮件失败:", e); mailVO.setStatus("fail"); mailVO.setError(e.getMessage()); return mailVO; } } @Override public void checkMail(MailVO mailVo) { if (StringUtils.isEmpty(mailVo.getEmailReciever())) { throw new RuntimeException("邮件收信人不能为空"); } if (StringUtils.isEmpty(mailVo.getEmailSubject())) { throw new RuntimeException("邮件主题不能为空"); } if (StringUtils.isEmpty(mailVo.getEmailText())) { throw new RuntimeException("邮件内容不能为空"); } } @Override public void sendMimeMail(MailVO mailVo) { try { //true表示支持复杂类型 MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); //邮件发信人从配置项读取 mailVo.setEmailSender(mailVo.getEmailSender()); //邮件发信人 messageHelper.setFrom(mailVo.getEmailSender()); //邮件收信人 messageHelper.setTo(mailVo.getEmailReciever().split(",")); //邮件主题 messageHelper.setSubject(mailVo.getEmailSubject()); //邮件主题 messageHelper.setText(mailVo.getEmailText()); //抄送 if (!StringUtils.isEmpty(mailVo.getCarbonCopy())) { messageHelper.setCc(mailVo.getCarbonCopy().split(",")); } //密送 if (!StringUtils.isEmpty(mailVo.getBlindCarbonCopy())) { messageHelper.setCc(mailVo.getBlindCarbonCopy().split(",")); } //添加邮件附件 if (mailVo.getMultipartFiles() != null) { for (MultipartFile multipartFile : mailVo.getMultipartFiles()) { messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile); } } //发送时间 if (!StringUtils.isEmpty(mailVo.getSentDate().toString())) { messageHelper.setSentDate(new Date()); } //正式发送邮件 mailSender.send(messageHelper.getMimeMessage()); mailVo.setStatus("ok"); logger.info("发送邮件成功:{}->{}", mailVo.getEmailSender(), mailVo.getEmailReciever()); } catch (Exception e) { //发送失败 throw new RuntimeException(e); } } @Override public MailVO saveMail(MailVO mailVo) { return mailVo; } }
2.配置文件
其中配置文件分内外网
使用外网(qq邮箱)
application.xml
spring: mail: host: smtp.qq.com #SMTP服务器地址 username: xxxx@qq.com #登陆账号 password: 1 #授权码 不是密码 default-encoding: utf-8 #utf-8格式 properties: mail: smtp: ssl: enable: true #一定要开启ssl,不然会503 验证失败 servlet: multipart: max-file-size: 10MB #限制单个文件大小 max-request-size: 50MB #限制请求总量
获取授权码
访问qq邮箱,访问左上角帮助中心
返回的就是授权码 复制到上文即可
内网邮箱
spring: mail: host: mail.xxx.com #SMTP服务器地址 port: 25 #内网需要指定端口需要指定 username: xxx@xx.com #登陆账号 password: xxxx #登陆密码 default-encoding: utf-8 properties: mail: smtp: ssl: enable: false #根据自己内网是否启动配置 servlet: multipart: max-file-size: 10MB #限制单个文件大小 max-request-size: 50MB #限制请求总量
然后就可以发送邮件了
3.接口入参
{ "emailSender":"xxxx@xxx.com", "emailReciever":"xxx@xxx.com", "emailSubject":"测试邮件", "emailText":"6月7日上午,2021年高考首场语文考试结束。澎湃新闻从教育部考试中心获悉,今年全国高考语文共有8套试卷,教育部考试中心命制4套,分别为全国甲卷、全国乙卷、新高考Ⅰ卷、新高考Ⅱ卷,北京、天津、上海、浙江。", "sentDate": 20210607 }