方案实现
1、引用依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、代码实现
此处以从**QQ邮箱**发送为例
public void send() throws MessagingException {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.qq.com");//发邮件主机地址
javaMailSender.setPort(587);//发邮件端口
javaMailSender.setUsername("314850828@qq.com");//发件人用户名。基本与发件人邮箱地址相同
javaMailSender.setPassword("gbpzwiprmqcbcace"); //授权码。这个不是你的QQ密码
javaMailSender.setDefaultEncoding("UTF-8");
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("Test Subject");
helper.setFrom("314850828@qq.com"); //发件人邮箱地址
helper.setTo("test@sina.com");
helper.setSentDate(new Date());
helper.setText("test la la la ", true);
javaMailSender.send(mimeMessage);
}