spring boot如何发送邮件呢?
发送邮件是我们日常开发中常用的操作方式,那么SpringBoot如何发送邮件呢?下文笔者将一一道来,如下所示
SpringBoot发送邮件的实现思路
1.引入相应的jar包 2.对邮件发送进行相应的信息配置 3.借助spring中发送邮件的核心代码进行邮件发送例
1、jar包引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、配置文件
spring.mail.host=smtp.qq.com spring.mail.username=88888@qq.com #这里切记 password是授权密码 授权密码 授权密码 不是你的邮箱登陆密码 spring.mail.password=123456 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
发送邮件接口定义
/** * 发送普通文本邮件 * 我们这里的普通邮件是指最为普通的纯文本邮件 * @param to 收件人 * @param subject 主题 * @param content 内容 */ void sendSimpleMail(String to, String subject, String content); /** * 发送HTML邮件 * HTML文件就是指在文件内容中可以添加<html>等标签, * 收件人收到邮件后显示内容也和网页一样 * @param to 收件人 * @param subject 主题 * @param content 内容(可以包含<html>等标签) */ void sendHtmlMail(String to, String subject, String content); /** * 发送带附件的邮件 * 带附件的邮件在HTML邮件上添加一些参数即可 * @param to 收件人 * @param subject 主题 * @param content 内容 * @param filePath 附件路径 */ void sendAttachmentMail(String to, String subject, String content, String filePath); /** * 发送带图片的邮件 * 带图片即在正文中使用<img>标签,并设置我们需要发送的图片,也是在HTML基础上添加一些参数 * @param to 收件人 * @param subject 主题 * @param content 文本 * @param rscPath 图片路径 * @param rscId 图片ID,用于在<img>标签中使用,从而显示图片 */ void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
实现---发送邮件接口
@Service("mailService") public class MailServiceImpl implements MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; @Async//异步 public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to);//收信人 message.setSubject(subject);//主题 message.setText(content);//内容 message.setFrom(from);//发信人 mailSender.send(message); } public void sendHtmlMail(String to, String subject, String content) { log.info("发送HTML邮件开始:{},{},{}", to, subject, content); //使用MimeMessage,MIME协议 MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper; //MimeMessageHelper帮助我们设置更丰富的内容 try { helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true);//true代表支持html mailSender.send(message); log.info("发送HTML邮件成功"); } catch (MessagingException e) { log.error("发送HTML邮件失败:", e); } } public void sendAttachmentMail(String to, String subject, String content, String filePath) { log.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper; try { helper = new MimeMessageHelper(message, true); //true代表支持多组件,如附件,图片等 helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件 mailSender.send(message); log.info("发送带附件邮件成功"); } catch (MessagingException e) { log.error("发送带附件邮件失败", e); } } public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) { log.info("发送带图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper; try { helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res);//重复使用添加多个图片 mailSender.send(message); log.info("发送带图片邮件成功"); } catch (MessagingException e) { log.error("发送带图片邮件失败", e); } } }
发送邮件测试
@RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { @Autowired private MailService mailService; /** * 发送简单纯文本邮件 */ @Test public void sendSimpleMail() { mailService.sendSimpleMail("receiver@email.com", "发送邮件测试", "大家好,这是我用springboot进行发送邮件测试"); } /** * 发送HTML邮件 */ @Test public void sendHtmlMail() { String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件" + "</font></h3></body></html>"; mailService.sendHtmlMail("receiver@email.com", "发送邮件测试", content); } /** * 发送带附件的邮件 */ @Test public void sendAttachmentMail() { String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有附件哦" + "</font></h3></body></html>"; String filePath = "your file path"; mailService.sendAttachmentMail("receiver@email.com", "发送邮件测试", content, filePath); } /** * 发送带图片的邮件 */ @Test public void sendInlineResourceMail() { String rscPath = "your picture path"; String rscId = "001"; String content = "<html><body><h3><font color=\"red\">" + "大家好,这是springboot发送的HTML邮件,有图片哦" + "</font></h3>" + "<img src=\'cid:" + rscId + "\'></body></html>"; mailService.sendInlineResourceMail("receiver@email.com", "发送邮件测试", content, rscPath, rscId); } }
常见错误说明
javax.mail.AuthenticationFailedException: 535 Error: private static final String USER = "xxxxxx@qq.com"; // 发件人称号,qq邮箱地址 private static final String PASSWORD = "16位数的授权码"; // qq邮箱,使用的户端授权码
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。