spring boot 发送邮件

来源:互联网 发布:淘宝装修设计师 编辑:程序博客网 时间:2024/06/06 02:08

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.comspring.mail.port=587spring.mail.username=你邮箱的用户名spring.mail.password=你邮箱的密码spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.timeout=180000spring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=truemail.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)@SpringBootTestpublic 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");    }}