springboot实现的邮件发送方式

application.yaml配置如下
[email protected]
spring.mail.password=foulosecxxmccsbad
spring.mail.host=smtp.qq.com
#开始加密认证
spring.mail.properties.mail.smtp.ssl.enable=true

package com.mail;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
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;
import java.util.Date;

@SpringBootTest
class Springboot10TestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;

简单邮件发送方式

@Test
void contextLoads() {
    SimpleMailMessage mailMessage=new SimpleMailMessage();
    mailMessage.setSubject("你好,我要测试Mial");
    mailMessage.setText("谢谢自己的坚持");
    mailMessage.setSentDate(new Date());
    mailMessage.setTo("[email protected]");
    mailMessage.setFrom("[email protected]");
    mailSender.send(mailMessage);

}

带附件等复杂邮件方式

@Test
void contextLoads2() throws MessagingException {
    MimeMessage mimeMessage=mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper=  new MimeMessageHelper(mimeMessage,true,"utf-8");
    mimeMessageHelper.setSubject("你好");
    mimeMessageHelper.setText("

坚持把

",true); mimeMessageHelper.addAttachment("test.txt",new File("C:\\Users\\Administrator\\Desktop\\test.txt")); mimeMessageHelper.setTo("[email protected]"); mimeMessageHelper.setFrom("[email protected]"); mailSender.send(mimeMessage); }

}

你可能感兴趣的:(springboot实现的邮件发送方式)