Spring Boot实现发送邮件

java发送邮件

一,准备工作

  • 如果是qq邮箱,首先需要开启smtp服务,还要获取授权码

Spring Boot实现发送邮件_第1张图片

二,添加依赖

  • spring boot整合了邮件所需的依赖,开箱即用
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

三,配置

  • 配置文件(我用的是application.properties)
#qq邮箱则写smtp.qq.com
spring.mail.host=smtp.qq.com
#发件人的邮箱名
spring.mail.username=xxxx@qq.com
#qq邮箱是授权码,不是qq邮箱的密码
spring.mail.password=xxxunbxxxxx
#其他配置
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true


四,实现

1. 先定义MailService 接口

public interface MailService {
    //简单邮件
    public void sendSimpleMail(String to, String subject, String content);
    //带附件邮件
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException;

}

2. MailServiceImpl实现MailService 接口(主要靠mailSender实现)

//此处只显示主要的导入
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

@Component
public class MailServiceImpl implements MailService {
    //注入mailSender,此处JavaMailSender类为系统自动生成,不用自己写
    @Autowired
    private JavaMailSender mailSender;

    //发送者
    @Value("${spring.mail.username}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 邮件发送者
        message.setTo(to); // 邮件接受者
        message.setSubject(subject); // 主题
        message.setText(content); // 内容

        mailSender.send(message);
    }
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = file.getFilename();
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

}

五,单元测试


@RunWith(SpringRunner.class)
@SpringBootTest
public class eamilDemoTest {
    @Autowired
    private MailService mailService;
    //简单邮件
    @Test
    public void testSimpleMail() {
        String to = "[email protected]";
        String subject = "这是一封测试邮件";
        String content = "hello email!";
        mailService.sendSimpleMail(to,subject,content);
        System.out.println("发送成功!");
    }

    //带附件邮件测试
    @Test
    public void testMailWithFile() throws MessagingException {
        String to = "[email protected]";
        String subject = "这是一封带附件的测试邮件";
        String content = "

看附件!

"
; String filePath = "D:/uploads/敏捷复习整理.docx"; mailService.sendAttachmentsMail(to,subject,content,filePath); System.out.println("发送成功!"); } }

六,测试结果

在这里插入图片描述
Spring Boot实现发送邮件_第2张图片

你可能感兴趣的:(java)