springboot-mail 发表于 2018-09-20 | 更新于: 2023-02-26 | 分类于 springboot 配置springboot发送简易右键 maven依赖123456<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency></dependencies> 编写service以及实现123456789101112131415161718192021222324252627282930//Servicepublic interface MailService { void sendSimpleMail(String to, String subject, String content);}//ServiceImpl@Component@Logpublic class MailServiceImpl implements MailService { @Resource private JavaMailSender javaMailSender; @Value("${mail.fromMail.addr}") private String from; @Override public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { javaMailSender.send(message); log.info("简单邮件已经发送。"); } catch (Exception e) { log.info("发送简单邮件时发生异常!"+e); } }} 配置必要属性设置123456789spring: mail: host: smtp.qq.com username: xzx@qq.com default-encoding: UTF-8 password: passwordmail: fromMail: addr: xzx@qq.com 测试1234567891011121314@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = WebsiteApplication.class)@WebAppConfigurationpublic class MailServiceTest { @Resource private MailService mailServiceImpl; @Test public void testSimpleMail() throws Exception { mailServiceImpl.sendSimpleMail("mir2285@outlook.com", "test simple mail", " hello this is simple mail"); }}