exp: SpringBoot发送邮件功能

如果是发送纯文本邮件不带附件,可以直接用SimpleMailMessage

例:
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.SimpleMailMessage;

@Autowired
private JavaMailSender mailSender;

public void sendSimpleTextMail(String from, String[] to, String subject, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        this.mailSender.send(message);
    }

如果需要发送带附件的邮件,必须用Spring的JavaMailSender及MimeMessage。
下面的代码展示了附件以及邮件模板的应用。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

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

@Service
public class MailTemplateService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    @Qualifier("email")
    private TemplateEngine templateEngine;

    /**
     * 发送不带附件的纯文本邮件
     * @param from
     * @param to
     * @param subject
     * @param content
     * @throws MessagingException
     */
    public void sendSimpleTextMail(String from, String[] to, String subject, String content)
            throws MessagingException {
        sendSimpleTextMailWithAttachments(from, to, subject,content, null);
    }
    /**
     * 发送带附件的纯文本邮件
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param attachments
     * @throws MessagingException
     */
    public void sendSimpleTextMailWithAttachments(String from, String[] to, String subject, String content, String[] attachments)
            throws MessagingException {
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        setMailSubjectFromTo(message, subject, from, to);
        message.setText(content);
        setMailAttachments(message,attachments);
        this.mailSender.send(mimeMessage);
    }

    /**
     * 根据thymeleaf模板发送不带附件的邮件
     * @param subject
     * @param from
     * @param to
     * @param templateName - 模板文件,需要放到 src/main/resources/mail/目录下,且后缀名必须为 .html
     * @param params - 模板文件中定义的参数
     * @throws MessagingException
     */
    public void sendTemplateMail(String subject, String from, String[] to,String templateName, Map<String, Object> params)
            throws MessagingException {
        sendTemplateMailWithAttachments(subject, from, to, templateName, params,null);
    }
    /**
     * 根据thymeleaf模板发送带附件的邮件
     * @param subject
     * @param from
     * @param to
     * @param templateName - 模板文件,需要放到 src/main/resources/mail/目录下,且后缀名必须为 .html
     * @param params - 模板文件中定义的参数
     * @param attachments -附件
     * @throws MessagingException
     */
    public void sendTemplateMailWithAttachments(String subject, String from, String[] to,String templateName, Map<String, Object> params,
                                    String[] attachments) throws MessagingException {
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        setMailSubjectFromTo(message, subject, from, to);
        setMailTemplate(message, templateName, params);
        setMailAttachments(message,attachments);
        this.mailSender.send(mimeMessage);
    }

    /**
     * 设置邮件的subject,from,to
     * @param message
     * @param subject
     * @param from
     * @param to
     * @throws MessagingException
     */
    private void setMailSubjectFromTo(MimeMessageHelper message,String subject, String from, String[] to)
            throws MessagingException {
        message.setSubject(subject);
        message.setFrom(from);
        message.setTo(to);
    }

    /**
     * 设置邮件的thymeleaf模板
     * @param message
     * @param templateName
     * @param params
     * @throws MessagingException
     */
    private void setMailTemplate(MimeMessageHelper message,String templateName, Map<String, Object> params)
            throws MessagingException {
        final Context context = new Context(Locale.CHINA);
        if(templateName!=null && !"".equals(templateName) && params!=null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                context.setVariable(entry.getKey(), entry.getValue());
            }
            // Create the HTML body using Thymeleaf
            final String htmlContent = this.templateEngine.process(templateName, context);
            message.setText(htmlContent, true); // true = isHtml
        }
    }

    /**
     * 设置邮件的附件
     * @param message
     * @param attachments
     * @throws MessagingException
     */
    private void setMailAttachments(MimeMessageHelper message, String[] attachments) throws MessagingException {
        if(attachments!=null) {
            for (String attachment : attachments) {
                FileSystemResource file = new FileSystemResource(new File(attachment));
                String fileName = attachment.substring(attachment.lastIndexOf(File.separator)+1);
                message.addAttachment(fileName, file);
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值