springboot发送邮件

maven依赖

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

yml配置

spring:
	mail:
    # 发送邮件参数配置
    host: smtp.qq.com
    username: [email protected]
    # qq邮件中心 设置--> 账户 --> 开启POP3/SMTP服务 (如何使用 Foxmail 等软件收发邮件?得到密码
    password: xxx
    properties:
      mail.smtp.auth: true
      mail.smtp.timeout: 2000
      mail.smtp.starttls.enable: true
      mail.smtp.starttls.required: true

接口实现

public interface IMailSender {

    /**
     * 发送普通邮件
	 * @param from 发送人
	 * @param to 接收人
	 * @param subject 邮件主题
	 * @param content 邮件内容
	 */
	boolean sendSimpleMail(String from, String to, String subject, String content);

	/**
     * HTML邮件
	 * @param from 发送人
     * @param to 接收人
     * @param subject 邮件主题
     * @param htmlContent HTML内容
	 */
	boolean sendHtmlMail(String from, String to, String subject, String htmlContent);

    /**
     * 带附件的邮件[单个附件]
	 * @param from 发送人
     * @param to 接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param filename 附件路径
	 */
	boolean sendAttachmentMail(String from, String to, String subject, String content, String filename);

    /**
     * 发送带多个附件
	 * @param from 发送人
	 * @param to 接收人
	 * @param subject 邮件主题
	 * @param content 邮件内容
     * @param paths 附件路径集合
	 */
	boolean sendMultiAttachmentsMail(String from, String to, String subject, String content, List<String> paths);
}

实现类

import cn.stylefeng.guns.mail.service.IMailSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.util.StringUtils;

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

/**
 * @Author: lf
 * @Description: 邮件发送实现类
 * @Date: 2020-5-12
 */
@Service
@Slf4j
public class MailSenderImpl implements IMailSender {

	@Autowired
	private JavaMailSender mailSender;

	@Override
	public boolean sendSimpleMail(String from,String to, String subject, String content) {
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(from);
		mailMessage.setTo(to);
		mailMessage.setSubject(subject);
		mailMessage.setText(content);
		mailSender.send(mailMessage);
		return true;
	}

	@Override
	public boolean sendHtmlMail(String from,String to, String subject, String htmlContent) {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(message, true);
			helper.setFrom(from);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(htmlContent, true);
		} catch (MessagingException e) {
			e.printStackTrace();
			log.error("html邮件发送失败: {}",e.getMessage());
			return false;
		}
		mailSender.send(message);
		return true;
	}

	@Override
	public boolean sendAttachmentMail(String from,String to, String subject, String content, String filename) {
		MimeMessage mimeMailMessage = mailSender.createMimeMessage();
		MimeMessageHelper mimeMessageHelper;

		try {
			mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content);
			if(!StringUtils.isEmpty(filename)) {
				FileSystemResource file = new FileSystemResource(new File(filename));
				mimeMessageHelper.addAttachment(file.getFilename(), file);
			}
		} catch (MessagingException e) {
			e.printStackTrace();
			log.error("单附件邮件发送失败: {}",e.getMessage());
			return false;
		}
		mailSender.send(mimeMailMessage);
		return true;
	}

	@Override
	public boolean sendMultiAttachmentsMail(String from, String to, String subject, String content, List<String> paths) {
		MimeMessage mimeMailMessage = mailSender.createMimeMessage();
		MimeMessageHelper mimeMessageHelper;
		try {
			mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content);
			if(null != paths && !paths.isEmpty()){
				for (String path : paths) {
					FileSystemResource file = new FileSystemResource(new File(path));
					mimeMessageHelper.addAttachment(file.getFilename(), file);
				}
			}
			mailSender.send(mimeMailMessage);
		} catch (MessagingException e) {
			e.printStackTrace();
			log.error("多个附件邮件发送失败: {}",e.getMessage());
			return false;
		}
		return true;
	}
}

拷贝及用,有特别需求的,自己看着改

你可能感兴趣的:(spring,邮件)