springboot邮件发送服务
2019-03-23 17:59
513 查看
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.properties
#qq邮箱 #spring.mail.host=smtp.qq.com #spring.mail.username=2456935718@qq.com #163邮箱 spring.mail.host=smtp.163.com spring.mail.username=q975144677@163.com #授权码 spring.mail.password=163sqm #spring.mail.properties.mail.smtp.auth.password=163sqm #spring.mail.port=25 #spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
使用
public class EmailServiceImpl implements EmailService{ @Autowired SpringTemplateEngine springTemplateEngine ; @Value("spring.mail.username") String sender; @Autowired JavaMailSender mailSender ; @Override public boolean sendEmail(Email email) { try { MimeMessage mm = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mm, true); helper.setFrom(sender); helper.setTo(email.getReceiveemail()); helper.setSubject(email.getSubject()); //html 转换为 thymeleaf Context context = new Context(); context.setVariable("email", email); String text = springTemplateEngine.process(email.getTemplate(), context); helper.setText(text, true); mailSender.send(mm); }catch (Exception e ){ e.printStackTrace(); } return false; } }
email 为 自己封装的类
email.getTemplate() 这里的template 需要在templates中事先写好 对应名字.html
如果不用application.properties 配置的话 , 可以自己new JavaMailSenderImpl 对象,然后自己配置 如
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.qq.com"); // mailSender.setPort(465); mailSender.setUsername("2118724165@qq.com"); mailSender.setPassword(""); //加认证机制 Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.auth", true); javaMailProperties.put("mail.smtp.starttls.enable", true); javaMailProperties.put("mail.smtp.timeout", 5000); mailSender.setJavaMailProperties(javaMailProperties); //创建邮件内容 SimpleMailMessage message=new SimpleMailMessage(); message.setFrom("2118724165@qq.com"); message.setTo(email); message.setSubject(subject); message.setText(msg); //发送邮件 mailSender.send(message); --------------------- 作者:李秀才 来源:CSDN 原文:https://blog.csdn.net/qq_33556185/article/details/51028952 版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章推荐
- 使用job自动抓取网页数据发送邮件,以springboot服务启动
- springboot 邮件发送实现
- spring-boot 速成(10) -【个人邮箱/企业邮箱】发送邮件
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Spring Boot 发送邮件功能案例分析
- SpringBoot之邮件发送
- Spring Boot(十二)集成spring-boot-starter-mail发送邮件
- Springboot发送邮件
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- SpringBoot中发送QQ邮件功能的实现代码
- Spring Boot 菜鸟教程 26 集成邮件发送
- spring boot发送短信服务
- Spring Boot Admin监控服务上下线邮件通知
- spring boot 发送邮件
- springboot 邮件发送
- Spring boot下实现邮件发送功能
- springboot发送邮件
- 使用Springboot Email实现邮件发送
- Springboot mail邮件服务功能学习
- Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件