SpringBoot中如何实现一个邮件发送功能

发布时间:2021-08-06 16:29:58 作者:Leah
来源:亿速云 阅读:114

SpringBoot中如何实现一个邮件发送功能,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

SpringBoot项目引入邮件包

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>

yml配置邮件相关

spring: mail: # 邮件服务地址 host: smtp.163.com # 端口,可不写默认 port: 25 # 编码格式 default-encoding: utf-8 # 用户名 username: xxx@163.com # 授权码,就是我们刚才准备工作获取的代码 password: xxx # 其它参数 properties:  mail:  smtp:   # 如果是用 SSL 方式,需要配置如下属性,使用qq邮箱的话需要开启   ssl:   enable: true   required: true   # 邮件接收时间的限制,单位毫秒   timeout: 10000   # 连接时间的限制,单位毫秒   connectiontimeout: 10000   # 邮件发送时间的限制,单位毫秒   writetimeout: 10000

针对于上面提的超时问题,捕获超时异常就可解决。

邮件发送工具类

主要通过以下工具类就可以满足发送java邮件的需要。当我们进行好 yml 配置后,SpringBoot会帮助我们自动配置 JavaMailSender 我们通过这个java类就可以实现操作java来发送邮件。以下列举了几种常用的邮件。

@Servicepublic class MailService { private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class); @Autowired private JavaMailSender mailSender; private static final String SENDER = "xxx@163.com"; /**  * 发送普通邮件  *  * @param to  收件人  * @param subject 主题  * @param content 内容  */ @Override public void sendSimpleMailMessge(String to, String subject, String content) {  SimpleMailMessage message = new SimpleMailMessage();  message.setFrom(SENDER);  message.setTo(to);  message.setSubject(subject);  message.setText(content);  try {   mailSender.send(message);  } catch (Exception e) {   logger.error("发送简单邮件时发生异常!", e);  } } /**  * 发送 HTML 邮件  *  * @param to  收件人  * @param subject 主题  * @param content 内容  */ @Override public void sendMimeMessge(String to, String subject, String content) {  MimeMessage message = mailSender.createMimeMessage();  try {   //true表示需要创建一个multipart message   MimeMessageHelper helper = new MimeMessageHelper(message, true);   helper.setFrom(SENDER);   helper.setTo(to);   helper.setSubject(subject);   helper.setText(content, true);   mailSender.send(message);  } catch (MessagingException e) {   logger.error("发送MimeMessge时发生异常!", e);  } } /**  * 发送带附件的邮件  *  * @param to  收件人  * @param subject 主题  * @param content 内容  * @param filePath 附件路径  */ @Override public void sendMimeMessge(String to, String subject, String content, String filePath) {  MimeMessage message = mailSender.createMimeMessage();  try {   //true表示需要创建一个multipart message   MimeMessageHelper helper = new MimeMessageHelper(message, true);   helper.setFrom(SENDER);   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);  } catch (MessagingException e) {   logger.error("发送带附件的MimeMessge时发生异常!", e);  } } /**  * 发送带静态文件的邮件  *  * @param to  收件人  * @param subject 主题  * @param content 内容  * @param rscIdMap 需要替换的静态文件  */ @Override public void sendMimeMessge(String to, String subject, String content, Map<String, String> rscIdMap) {  MimeMessage message = mailSender.createMimeMessage();  try {   //true表示需要创建一个multipart message   MimeMessageHelper helper = new MimeMessageHelper(message, true);   helper.setFrom(SENDER);   helper.setTo(to);   helper.setSubject(subject);   helper.setText(content, true);   for (Map.Entry<String, String> entry : rscIdMap.entrySet()) {    FileSystemResource file = new FileSystemResource(new File(entry.getValue()));    helper.addInline(entry.getKey(), file);   }   mailSender.send(message);  } catch (MessagingException e) {   logger.error("发送带静态文件的MimeMessge时发生异常!", e);  } }}

发送邮件的demo

@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringbooEmailDemoApplicationTests { @Autowired private MailService mailService; private static final String TO = "xxx@qq.com"; private static final String SUBJECT = "测试邮件"; private static final String CONTENT = "test content"; /**  * 测试发送普通邮件  */ @Test public void sendSimpleMailMessage() {  mailService.sendSimpleMailMessge(TO, SUBJECT, CONTENT); } /**  * 测试发送html邮件  */ @Test public void sendHtmlMessage() {  String htmlStr = "<h2>Test</h2>";  mailService.sendMimeMessge(TO, SUBJECT, htmlStr); } /**  * 测试发送带附件的邮件  * @throws FileNotFoundException  */ @Test public void sendAttachmentMessage() throws FileNotFoundException {  File file = ResourceUtils.getFile("classpath:test.txt");  String filePath = file.getAbsolutePath();  mailService.sendMimeMessge(TO, SUBJECT, CONTENT, filePath); } /**  * 测试发送带附件的邮件  * @throws FileNotFoundException  */ @Test public void sendPicMessage() throws FileNotFoundException {  String htmlStr = "<html><body>测试:图片1 <br> <img src=\'cid:pic1\'/> <br>图片2 <br> <img src=\'cid:pic2\'/></body></html>";  Map<String, String> rscIdMap = new HashMap<>(2);  rscIdMap.put("pic1", ResourceUtils.getFile("classpath:pic01.jpg").getAbsolutePath());  rscIdMap.put("pic2", ResourceUtils.getFile("classpath:pic02.jpg").getAbsolutePath());  mailService.sendMimeMessge(TO, SUBJECT, htmlStr, rscIdMap); }}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. Springboot实现邮件发送功能
  2. 利用JavaMail怎么实现一个邮件发送功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:java中怎么随机生成验证码图片

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》