SpringBoot 发送邮件 | Eddie'Blog
SpringBoot 发送邮件

SpringBoot 发送邮件

eddie 546 2020-10-13

E-Mail

一、添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

二、配置

application.yml

spring:
  mail:
    host: smtp.qq.com
    port: 587
    username: xxxxxxxx@qq.com  # 发送的邮件账户
    password: uugmjyaxxxxxxxx  # 授权码
    protocol: smtp
    default-encoding: UTF-8
    properties:
      mail.smtp.auth: true
      mail.smtp.starttls.enable: true
      mail.smtp.starttls.required: true
      mail.smtp.socketFactory.port: 465
      mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
      mail.smtp.socketFactory.fallback: false

三、工具类

package com.common.util;

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author eddie.lee
 * @className MailUtils 
 * @description 
 * @date created in 2020-09-28 13:48
 * @modified by
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MailUtils {

	private final JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")
	private String username;

	/**
	 * 发送文本邮件
	 * 
	 * @param to
	 * @param subject
	 * @param content
	 */
	public void sendTextMail(String to, String subject, String content) {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(username);
		simpleMailMessage.setTo(to);
		simpleMailMessage.setSubject(subject);
		simpleMailMessage.setText(content);
		javaMailSender.send(simpleMailMessage);
	}

	/**
	 * 发送网页邮件
	 * 
	 * @param to
	 * @param subject
	 * @param content
	 */
	public void sendHtmlMail(String to, String subject, String content) {
		MimeMessage message = javaMailSender.createMimeMessage();
		MimeMessageHelper messageHelper;
		try {
			messageHelper = new MimeMessageHelper(message, true);
			messageHelper.setFrom(username);
			messageHelper.setTo(to);
			messageHelper.setSubject(subject);
			messageHelper.setText(content, true);
			javaMailSender.send(message);
		} catch (MessagingException e) {
			log.error("发送邮件时发生异常: ", e);
		}
	}

	/**
	 * 发送附件的邮件
	 * 
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 */
	public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
		MimeMessage message = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(username);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);
			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
			helper.addAttachment(fileName, file);
			javaMailSender.send(message);
			// 日志信息
		} catch (MessagingException e) {
			log.error("发送邮件时发生异常: ", e);
		}
	}

}

四、测试类

package com.test;

import com.common.util.MailUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author eddie.lee
 * @className MailTest 
 * @description 
 * @date created in 2020-09-28 13:48
 * @modified by
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {

    @Autowired
    private MailUtils mailUtils;

    @Test
    public void test1(){
        mailUtils.sendTextMail("xxxxx@qq.com","主题"," 内容");
    }

}