您好,登錄后才能下訂單哦!
這篇文章主要講解了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。