本⽂实例为⼤家分享了springboot实现发送邮件的具体代码,供⼤家参考,具体内容如下
1.引⼊依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3.password⾥输⼊你获取到的密码(重点:这⾥⼀定不能错)
spring.mail.username=*****************
spring.mail.password=********
spring.mail.host=smtp.qq
#开启加密验证
spring.mail.properties.mail.able=true
4.EmailApplicationTests类⾥进⾏测试, contextLoads为简单的发送,仅限于⽂本。contextLoads2为复杂,可以发送图⽚,html格式j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class EmailApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
qq怎么发邮件SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好");
mailMessage.setText("123456");
mailMessage.setTo("*****************");
mailMessage.setFrom("*****************");
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage = ateMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("你好");
helper.setText("<p style='color:red'>这是红⾊的</p>",true);
helper.addAttachment("bg.jpg",new File("D:\\money\\⽹页\\疫情\\images\\bg1.jpg")); helper.setTo("*****************");
helper.setFrom("*****************");
mailSender.send(mimeMessage);
}
}
5.分别对应的结果
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
发布评论