您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“SpringBoot怎么实现发送QQ邮件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot怎么实现发送QQ邮件”吧!
如果是Spring Boot应用不需要指定版本号了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
如下配置
在配置前获取QQ邮箱授权码;QQ邮件在Spring的Bean中密码为授权码。
@Configuration public class MailConfig { @Bean public JavaMailSender getJavaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); // mailSender.setHost("smtp.gmail.com"); mailSender.setHost("smtp.qq.com"); mailSender.setPort(587); mailSender.setUsername("aihehe123@foxmail.com"); mailSender.setPassword("这里是QQ邮箱的授权码"); Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true"); return mailSender; } }
发送普通文本
SimpleMailMessage simpleMessage = new SimpleMailMessage(); simpleMessage.setFrom("aihehe123@foxmail.com"); simpleMessage.setTo("aihehe123@foxmail.com"); simpleMessage.setSubject("test"); simpleMessage.setText("hello"); javaMailSender.send(simpleMessage);
发送带附件的文本
MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true); mimeMessageHelper.setFrom("aihehe123@foxmail.com"); mimeMessageHelper.setTo("aihehe123@foxmail.com"); mimeMessageHelper.setSubject("test"); mimeMessageHelper.setText("hello"); String path = "http://gif.aihe.space/dd467677ea35485da3d12326fcf36e5d.gif"; UrlResource fileUrlResource = new UrlResource(path); mimeMessageHelper.addAttachment(UrlUtils.parseUrlName(path), fileUrlResource); javaMailSender.send(message);
前两个邮件是带附件的,最下面一个是不带附件的;
1、配置发送邮件的模板
public class MailUtils { public static SimpleMailMessage makeSimpleMessage(String subject,String text){ SimpleMailMessage simpleMessage = new SimpleMailMessage(); simpleMessage.setFrom("aihehe123@foxmail.com"); simpleMessage.setTo("aihehe123@foxmail.com"); simpleMessage.setSubject(subject); simpleMessage.setText(text); return simpleMessage; } }
2、应用启动通知
@Component @Slf4j public class ApplicationStartHandler implements ApplicationListener<ContextRefreshedEvent> { @Autowired JavaMailSender javaMailSender; @Override public void onApplicationEvent(ContextRefreshedEvent event) { javaMailSender.send(MailUtils.makeSimpleMessage("应用启动通知","")); log.info("app start mail sended"); } }
3、应用停止通知;
public class XXXApplication { @Autowired JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(XXXApplication.class, args); } @PreDestroy public void onExit() { log.info("###STOPing###"); javaMailSender.send(MailUtils.makeSimpleMessage("应用停止通知", "")); log.info("###STOP FROM THE LIFECYCLE###"); } }
感谢各位的阅读,以上就是“SpringBoot怎么实现发送QQ邮件”的内容了,经过本文的学习后,相信大家对SpringBoot怎么实现发送QQ邮件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。