spring boot 发送邮件

spring boot 发送邮件

发送邮件是一个很常用的功能,比如线上故障告警,验证码等功能都会用到,下面我们来看看用spring mail 来实现发送邮件

package org.xxz.util;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.Component;

@Slf4j
@Component
@ConfigurationProperties(prefix = "mail")
public class MailUtil {

    @Autowired
    private JavaMailSender sender;

    @Getter @Setter
    private String from;

    /**
     * 发送文本邮件
     * @param to 接受人
     * @param subject 主题
     * @param text 文本
     */
    public void sendText(String to, String subject, String text) throws MessagingException {
        log.info("===>to:{}, subject:{}, text:{}", to, subject, text);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        sender.send(message);
        log.info("===>send text mail finish");
    }

    /**
     * 发送html邮件
     * @param to
     * @param subject
     * @param text
     * @throws MessagingException
     */
    public void sendHtml(String to, String subject, String text) throws MessagingException {
        log.info("===>to:{}, subject:{}, text:{}", to, subject, text);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true);
        sender.send(message);
        log.info("===>send html mail finish");
    }

    /**
     * 发送附件邮件
     * @param to
     * @param subject
     * @param text
     * @param filePath
     * @throws MessagingException
     */
    public void sendAttachments(String to, String subject, String text, String filePath) throws MessagingException  {
        log.info("===>to:{}, subject:{}, text:{}, filePath:{}", to, subject, text, filePath);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String attachmentFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
        helper.addAttachment(attachmentFilename, file);
        sender.send(message);
        log.info("===>send attachments mail finish");
    }

    /**
     * 发送嵌入资源(一般是图片)邮件
     * @param to
     * @param subject
     * @param text 带图片的邮件<img src=\"cid:resId1\">
     * @param resPath 文件路径
     * @param resId 静态资源id
     * @throws MessagingException
     */
    public void sendInlineResource(String to, String subject, String text, String resPath, String resId) throws MessagingException {
        log.info("===>to:{}, subject:{}, text:{}, resPath:{}, resId:{}", to, subject, text, resPath, resId);
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true);

        FileSystemResource resource = new FileSystemResource(new File(resPath));
        helper.addInline(resId, resource);
        sender.send(message);
        log.info("===>send inline resource mail finish");
    }

}

我们在看看配置文件怎么配置(application.properties)

spring.mail.host=smtp-mail.outlook.com
spring.mail.port=587
spring.mail.username=你邮箱的用户名
spring.mail.password=你邮箱的密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=180000
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
mail.from=你邮箱的用户名

这里我用的是outlook

host 根据不同邮件服务商进行修改

我们来看看测试类

package org.xxz.util;

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;
import org.xxz.blog.util.MailUtil;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailUtilTest {

    @Autowired
    MailUtil mailUtil;

    String to = "修改成要接受邮件的邮箱地址";

    @Test
    public void testSendText() throws Exception {
        mailUtil.sendText(to, "测试text", "测试text");
    }

    @Test
    public void testSendHtml() throws Exception {
        mailUtil.sendHtml(to, "测试html", "<h1>测试html</h1>");
    }

    @Test
    public void testSendAttachments() throws Exception {
        mailUtil.sendAttachments(to, "测试attachments", "测试attachments", "d:\\attachments.txt");
    }

    @Test
    public void testSendInlineResource() throws Exception {
        // 注意图片的src=cid:xxx 与后面的要对应
        mailUtil.sendInlineResource(to, "测试inlineResource", "测试inline resource<img src=\"cid:001\">", "d:\\ihehe.jpg", "001");
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值