springboot 发送,简单,html格式,带本地附件,带远程附件邮件详解

1、导入相应的jar包

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

2、配置参数

mail:
    host: smtp.qq.com #发送邮件服务器
    username: [email protected] #发送邮件的邮箱地址
    password:  xxxx #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
    properties.mail.smtp.port: 465 #端口号465或587
    from: [email protected] # 发送邮件的地址,和上面username一致可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8

3、检查相应的邮件服务器是否开启服务

4、编写相应的工具

package top.cfl.cflwork.util;

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.Component;

import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.net.URL;

/**
 * @Author: fly 275300091
 * @Description: 邮件工具
 * @Date: Create in 2019/08/24
 */
@Component
public class MailUtil {

    /**
     * Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用
     */
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 配置文件中我的qq邮箱
     */
    @Value("${spring.mail.from}")
    private String from;

    /**
     * 简单文本邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        try {
            //创建SimpleMailMessage对象
            SimpleMailMessage message = new SimpleMailMessage();
            //邮件发送人
            message.setFrom(from);
            //邮件接收人
            message.setTo(to);
            //邮件主题
            message.setSubject(subject);
            //邮件内容
            message.setText(content);
            //发送邮件
            mailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * html邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendHtmlMail(String to, String subject, String content) {
        //获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(to);
            //邮件主题
            message.setSubject(subject);
            //邮件内容,html格式
            messageHelper.setText(content, true);
            //发送
            mailSender.send(message);
            //日志信息
            System.out.println("邮件已经发送。");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("发送邮件时发生异常!");
        }
    }

    /**
     * 带附件的邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            //本地或项目资源
            //FileSystemResource file = new FileSystemResource(new File(filePath));
            //FileDataSource fds = new FileDataSource(filePath);
            //远程资源
            URLDataSource uds=new URLDataSource(new URL(filePath));
            helper.addAttachment(uds.getName(), uds);
            mailSender.send(message);
            //日志信息
            System.out.println("邮件已经发送成功,带有附件。");
        } catch (Exception e) {
            System.err.println("发送邮件时发生异常!");
        }

    }

}

5、测试

public static void main(String[] args) {
        MailUtil mailUtil = new MailUtil();
//        mailUtil.sendSimpleMail("[email protected]","主题:简单邮件测试","内容:简单邮件测试");
        mailUtil.sendAttachmentsMail("[email protected]","主题:我是带有附件的,带有html格式","<h1>内容:我是带有附件的</h1>","http://www.xxx.com/upload/avatar//2019/0623/6b50262a7be6442d.jpg");
       
    }

猜你喜欢

转载自blog.csdn.net/xljx_1/article/details/100057073