Springboot之整合mail
前言
Spring Email抽象的核心是MailSender接口,MailSender的实现能够将想要发送的邮件通过邮件服务器发送到用户邮箱,实现发送邮箱的功能。
Spring 自带了一个 MailSender 的实现 JavaMailSenderImpl,它会使用 JavaMail API 来发送 Email。Spring 或 SpringBoot 应用在发送 Email 之前,我们必须要 JavaMailSenderImpl 装配为 Spring应用上下文的一个 bean。
配置
pom配置
<!--引入mail依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.properties文件配置
- qq邮箱
# JavaMailSender 邮件发送的配置 spring.mail.host=smtp.qq.com spring.mail.username=用户qq邮箱 #QQ邮箱的授权码 spring.mail.password=授权码 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.default-encoding=UTF-8
- 163邮箱
spring.mail.host=smtp.163.com spring.mail.username=用户163邮箱 spring.mail.password=邮箱密码 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.default-encoding=UTF-8
host 属性默认是 JavaMail 会话的主机;port 端口默认监听标准的 SMTP 端口25;如果邮件服务器需要认证的,还需要设置 userrname 和 password。
通常username是邮箱号,password为邮箱的授权码。授权码可以登录邮箱获取。
设置->账户->POP3/SMTP服务:开启服务后会获得的授权码.
EmailConfig实体类
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @Author youjp * @Description //TODO * @throw **/ @Component public class EmailConfig { @Value("${spring.mail.username}") private String emailForm; public String getEmailForm() { return emailForm; } public void setEmailForm(String emailForm) { this.emailForm = emailForm; } }
EmailService
package com.example.email.com.example.email.demo; /** * @Param * @return * @Author youjp * @Description //TODO * @throw **/ public interface EmailService { //发送简单的邮件 void sendMailSimpleMail(String sendTo,String title,String content); //发送html的邮件 void sendHtmlMail(String sendTo,String title,String content); //发送带附件的邮件 void sendAttachmentMail(String sendTo, String title, String content); }
EmailService实现类
package com.example.email.com.example.email.demo; import org.springframework.beans.factory.annotation.Autowired; 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.internet.MimeMessage; import java.io.File; /** * @Param * @return * @Author youjp * @Description //TODO * @throw **/ @Service public class EmailServiceImpl implements EmailService{ @Autowired private EmailConfig emailConfig; @Autowired private JavaMailSender mailSender; /** * @Param [sendTo, title, content] * @return void * @Author youjp * @Description //TODO 发送简单邮件 * @throw **/ @Override public void sendMailSimpleMail(String sendTo, String title, String content) { //简单邮件的发送 SimpleMailMessage message= new SimpleMailMessage(); message.setFrom(emailConfig.getEmailForm()); message.setTo(sendTo); message.setSubject(title); message.setText(content); mailSender.send(message); } /** * @Param [sendTo, title, content] * @return void * @Author youjp * @Description //TODO 发送一个带html格式的邮件 * @throw **/ @Override public void sendHtmlMail(String sendTo, String title, String content) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(emailConfig.getEmailForm()); mimeMessageHelper.setTo(sendTo); mimeMessageHelper.setSubject(title); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>"+content+"</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); mailSender.send(mimeMailMessage); } catch (Exception e) { e.printStackTrace(); } } /** * @Param [sendTo, title, content] * @return void * @Author youjp * @Description //TODO 发送带附件格式的邮件 * @throw **/ @Override public void sendAttachmentMail(String sendTo, String title, String content) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(emailConfig.getEmailForm()); mimeMessageHelper.setTo(sendTo); mimeMessageHelper.setSubject(title); mimeMessageHelper.setText(content); //文件路径 FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/a2.jpeg")); mimeMessageHelper.addAttachment("mail.png", file); mailSender.send(mimeMailMessage); } catch (Exception e) { e.printStackTrace(); } } }
EmailController 控制层
package com.example.email.com.example.email.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class EmailController { @Autowired private EmailService emailService; @RequestMapping("/simple") @ResponseBody public String sendSimpleEmail(String sendTo) { emailService.sendMailSimpleMail(sendTo, "发送简单邮件", "你好"); return "success"; } @RequestMapping("/sendHtmlMail") @ResponseBody public String sendHtmlMail(String sendTo) { emailService.sendHtmlMail(sendTo,"发送一个带html格式的邮件", "这是一封带html格式的邮件"); return "success"; } @RequestMapping("/sendAttachMail") @ResponseBody public String sendAttachMail(String sendTo) { emailService.sendAttachmentMail(sendTo,"发送带附件格式的邮件", "这是一封发送带附件格式的邮件"); return "success"; } }
请求接口:
请求发送带附件格式的邮件
其他请求方式一样。
有兴趣的老爷,可以关注我的公众号【一起收破烂】,回复【006】获取2021最新java面试资料以及简历模型120套哦~