摘要:1.pop.xml org.springframework.boot spring-boot-starter-mail 2.application.properties #邮件配置: spring.mail.host=smtp.qq.com spring.mail.username=发送人的邮箱@qq.com spring.mail.password=密
1.pop.xml
<!-- 邮件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 2.application.properties
#邮件配置: spring.mail.host=smtp.qq.com spring.mail.username=发送人的邮箱@qq.com spring.mail.password=密码(授权码,具体看下面的操作) spring.mail.properties.mail.smtp.auth = true spring.mail.properties.mail.smtp.starttls.enable = true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enable = true
先看看qq邮箱或者其他邮箱pop3服务开了没有。没有的去开启
3.测试类,只实现简单的邮件
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.internet.MimeMessage; @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Autowired JavaMailSender sender; @Test public void sendSimpleMail() throws Exception { MimeMessage message = null; boolean flag = true; try { message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("发送人的邮箱@qq.com"); helper.setTo("接受人的邮箱@qq.com"); helper.setSubject("邮件主题"); helper.setText("内容"); sender.send(message); } catch (Exception e) { flag = false; System.out.println(e); } System.out.println("flag:"+flag); } }
4.接收结果,图片就不马赛克了,不要乱搞哦
相关文章推荐
已有家长中招!“录取通知书”邮件暗藏骗局2022-09-06
连接企微和钉钉,新网全球邮助力企业协同办公2022-09-27
快速申请搭建免费企业邮箱的教程2022-09-16
如何避免你的外贸开发信被当成垃圾邮件?2022-09-14
2021年,全国企业邮箱用户共收发邮件约7637.7亿封2022-09-13