spring boot 项目搭建
link
项目创建后,怎么将 application.properties文件转换为格式为yml文件.
link
properties转yml
idea spring boot 热部署
JavaMail是什么
拓展阅读
JavaMail API简介
JavaMail API是读取、撰写、发送电子信息的可选包。我们可用它来建立如Eudora、Foxmail、MS Outlook Express一般的邮件用户代理程序(Mail User Agent,简称MUA)。而不是像sendmail或者其它的邮件传输代理(Mail Transfer Agent,简称MTA)程序那样可以传送、递送、转发邮件。从另外一个角度来看,我们这些电子邮件用户日常用MUA程序来读写邮件,而MUA依赖着MTA处理邮件的递送。
在清楚了到MUA与MTA之间的关系后,让我们看看JavaMail API是如何提供信息访问功能的吧!JavaMail API被设计用于以不依赖协议的方式去发送和接收电子信息,这个API被分为两大部分:
基本功能:如何以不依赖于协议的方式发送接收电子信息,这也是本文所要描述的,不过在下文中,大家将看到这只是一厢情愿而已。
第二个部分则是依赖特定协议的,比如SMTP、POP、IMAP、NNTP协议。在这部分的JavaMail API是为了和服务器通讯,并不在本文的内容中。
相关协议一览
在我们步入JavaMail API之前,先看一下API所涉及的协议。以下便是大家日常所知、所乐于使用的4大信息传输协议:
SMTP
POP
IMAP
MIME
示例
application.properties
spring.mail.host =smtp.sina.com spring.mail.port =465 spring.mail.username =skybox2022@sina.com spring.mail.password =**** spring.mail.protocol =smtps spring.mail.properties.mail.smtp.ssl.enable =true ```` 2. 发送邮件util类 ```java package com.nowcoder.community.util; //新浪邮箱的客户端 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @Component public class MailClient { private static final Logger logger = LoggerFactory.getLogger(MailClient.class); @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; /** * * @param to 收件人邮箱地址 * @param subject 发送主题 * @param content 发送的消息 */ public void sendMail(String to,String subject,String content){ // 创建消息主体 MimeMessage message = mailSender.createMimeMessage(); // MimeMessageHelper helper = new MimeMessageHelper(message); try { helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); mailSender.send(helper.getMimeMessage()); } catch (MessagingException e) { logger.error("发送邮件失败 :"+e.getMessage()); } } }
使用
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes=CommunityApplication.class) public class MailTests { @Autowired private MailClient mailClient; @Test public void testSendMail () { mailClient.sendMail("2324598422@qq.com" ,"test" ,"你好吗" ); } }
结合模板引擎发送 特定木板的邮件
定义邮件模板
<!DOCTYPE html > <html lang ="en" xmlns:th ="http://www.thymeleaf.org" > <head > <meta charset ="UTF-8" > <title > $Title$</title > </head > <body > <p > 欢迎你,<span style ="color:red;" th:text ="${username}" > </span > !</p > </body > </html >
结合thymeleaf模板引擎生成指定的邮件内容
@Autowired private MailClient mailClient; @Autowired private TemplateEngine templateEngine; @Test public void testHtmlMail () { Context context= new Context(); context.setVariable("username" ,"sandy" ); String content = templateEngine.process("/mail/demo" ,context); System.out.println(content); mailClient.sendMail("2324598422@qq.com" ,"test2" ,content); }