欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot JavaMailSender发送邮件功能

程序员文章站 2023-12-13 11:11:16
本文实例为大家分享了springboot javamailsender发送邮件的具体代码,供大家参考,具体内容如下 引入maven依赖包

本文实例为大家分享了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);
 }
}

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

上一篇:

下一篇: