当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot JavaMailSender发送邮件功能

SpringBoot JavaMailSender发送邮件功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了springboot javamailsender发送邮件的具体代码,供大家参考,具体内容如下

引入maven依赖包

<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-mail</artifactid>
</dependency>

163邮箱

application.properties

#####163邮箱########
spring.mail.host=smtp.163.com
spring.mail.username=*****@163.com
#163邮箱密码
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

运行类:

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.boot.test.context.springboottest;
import org.springframework.mail.simplemailmessage;
import org.springframework.mail.javamail.javamailsender;
import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)
@springboottest(classes=application.class)
public class my163mailtest {

 @autowired
 private javamailsender javamailsender;

 @value("${spring.mail.username}")
 private string username;

 @test
 public void testsendsimple() {
 simplemailmessage message = new simplemailmessage();
 message.setfrom(username);
 message.setto("*******@qq.com");
 message.setsubject("标题:测试标题");
 message.settext("测试内容部份");
 javamailsender.send(message);
 }
}

qq邮箱和163邮箱的区别是需要设置授权码而不是密码,具体操作参考:

application.properties

######qq邮箱########
spring.mail.host=smtp.qq.com
spring.mail.username=******@qq.com
#qq邮箱授权码
spring.mail.password=xuojxtkdojvzbhjj
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

运行类:

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.boot.test.context.springboottest;
import org.springframework.mail.simplemailmessage;
import org.springframework.mail.javamail.javamailsender;
import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)
@springboottest(classes=application.class)
public class myqqmailtest {

 @autowired
 private javamailsender javamailsender;

 @value("${spring.mail.username}")
 private string username;

 @test
 public void testsendsimple() {
 simplemailmessage message = new simplemailmessage();
 message.setfrom(username);
 message.setto("******@qq.com");
 message.setsubject("标题:测试标题");
 message.settext("测试内容部份");
 javamailsender.send(message);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网