话不多说,直接上代码吧。
1.引入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
2.全局环境配置
#邮件发送配置
spring.mail.host=smtp.qq.com
spring.mail.username=xxx@qq.com //发送方的邮箱
spring.mail.password=密码 // 对于qq邮箱,密码指的是授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.sender=xxx@qq.com
开始编码:
3. EmailService
public interface EmailService {
/**
* 简单邮件
* @param sendto
* @param title
* @param content
*/
void Sender(String sendto, String title, String content);
/**
* 带附件邮件发送
* @param sendto
* @param title
* @param content
* @param file 文件
*/
void sendAttachementFileMail(String sendto, String title, String content, File file);
}
4.ImpEmailService service接口实现
* @创建人:
* @创建时间: 2019/4/18
* @描述: 带附件邮件发送
*/
@Service
public class ImpEmailService implements EmailService {
@Value("${spring.mail.sender}")
private String sender;
@Autowired
private JavaMailSender mailSender;
/**
* @创建人:
* @创建时间: 2019/4/18
* @描述: 带附件邮件发送
*/
@Override
public void Sender(String sendto, String title, String content) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//发件人
simpleMailMessage.setFrom(sender);
//发送给谁
simpleMailMessage.setTo(sendto);
//邮件主题
simpleMailMessage.setSubject(title);
//邮件内容
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);
}
@Override
public void sendAttachementFileMail(String sendto, String title, String content, File file) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
//发件人
messageHelper.setFrom(sender);
messageHelper.setTo(sendto);
messageHelper.setSubject(title);
messageHelper.setText(content);
System.out.println("发件人:" + sender + "\n" +
"收件人:" + sendto + "\n" +
"标题:" + title + "\n" +
"内容:" + content);
//附件
FileSystemResource fileSystemResource = new FileSystemResource(file);
String fileName = file.getName();
System.out.println(fileName);
messageHelper.addAttachment(fileName, fileSystemResource);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(mimeMessage);
}
5. Controller
public class EmailController {
@Autowired
EmailService emailService;
@Autowired
private JavaMailSender mailSender;
@PutMapping("/sendEmail")
public String sendMail(){
emailService.Sender("xxx@qq.com", "Spring Boot邮件功能测试", "怎么更换密保手机?+" \n" +1、QQ能登录,密保手机、QQ安全中心手机版中的任何一种可用时,您可以登录QQ安全中心官网->密保手机->更换、通过统一安全验证->更换密保手机审核时间过后,即可更换;+" \n" +
" \"2、若您密保手机不在使用,请您进入QQ安全中心官网并登录您的QQ帐号->密保手机->点击“更换”->我已换号->输入新手机号码->更换其他验证方式,通过资料验证即可更换。+" \n" +
" \"注:若验证不通过,请继续邀请好友辅助,我们会根据反馈结果重新审核,结果将在4个小时内下发至填写的联系方式,请您留意。");
return "发送成功";
}
@PutMapping("/sendFileEmail")
public String sendFileMail(){
File file=new File("E:/log/spring.log");
emailService.sendAttachementFileMail("xxx@qq.com",
"Spring Boot带附件邮件功能测试", "请注意查收附件!", file);
return "带附件邮件发送成功!";
}
}
在PostMan 中测试,
带附件邮件测试
http://localhost:8080/sendFileEmail
简单邮件测试
http://localhost:8080/sendEmail
是正常的, 可以正常发出邮件。