基于spring boot java 邮箱功能

  • 时间:
  • 来源:新网

   

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服务开了没有。没有的去开启


然后点下面的生成授权码,这个要配置在上面的password那里,不要用你邮箱登录密码去配置!!!

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.接收结果,图片就不马赛克了,不要乱搞哦