开通邮件发送服务
配置文件
# 邮箱发送服务配置 spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=你的邮箱号码 spring.mail.password=授权码 spring.mail.default-encoding=utf-8 spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000
配置类
package com.dam.config.mail; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * 读取yml配置文件里面的信息 */ @Configuration @ConfigurationProperties(prefix = "spring.mail") @Data public class MailConfig { // private String email; private String host; private String port; private String username; private String password; }
Dto类
package com.dam.model.dto.third_party; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class EmailDto { /** * 接收邮箱 */ private String to; /** * 主题 */ private String subject; /** * 内容 */ private String content; }
Service
package com.dam.service; import com.dam.model.dto.third_party.EmailDto; public interface EmailService { /** * 发送邮件 * * @param emailDto 邮箱列表 */ boolean send(EmailDto emailDto); }
package com.dam.service.impl; import com.dam.config.mail.MailConfig; import com.dam.model.dto.third_party.EmailDto; import com.dam.service.EmailService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Service; @Slf4j @Service @RequiredArgsConstructor public class EmailServiceImpl implements EmailService { @Autowired private MailConfig mailConfig; @Autowired private JavaMailSenderImpl mailSender; @Override public boolean send(EmailDto emailDto) { String fromEmail = mailConfig.getUsername(); //定制纯文本邮件信息SimpleMailMessage SimpleMailMessage message = new SimpleMailMessage(); try { //设置发件箱 message.setFrom(fromEmail); //设置收件箱 message.setTo(emailDto.getTo()); //设置邮件主题 message.setSubject(emailDto.getSubject()); //设置邮件内容 message.setText(emailDto.getContent()); //调用Java封装好的发送方法 mailSender.send(message); return true; } catch (Exception e) { System.out.println("邮件发送失败,原因:" + e.getMessage()); return false; } } }
Controller
package com.dam.controller; import com.dam.model.dto.third_party.EmailDto; import com.dam.model.enums.ResultCodeEnum; import com.dam.model.result.R; import com.dam.service.EmailService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope @RequestMapping("/thirdParty/mail") @Api(tags = "邮件服务") public class MailController { @Autowired private EmailService emailService; @ApiOperation(value = "发送邮件") @PostMapping("/send") public R send(@RequestBody EmailDto emailDto) { boolean isSuccess = emailService.send(emailDto); if (isSuccess) { return R.ok(); } else { return R.error(ResultCodeEnum.FAIL); } } }