springboot发送邮件(含附件)

引入maven

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

 

  

yml配置

spring:
  mail:
    host: smtp.163.com    #邮件服务器地址,邮箱不一样,服务器地址不一样
    username:  #邮箱用户名 
    password:  #一般使用授权码
    properties:
      'mail.smtp.ssl.enable': 'true'   #设置安全连接

 

  

邮件发送附件实体类

MailAttachmentDto.java

import lombok.Data;
import lombok.experimental.Accessors;


@Data
@Accessors(chain = true)
public class MailAttachmentDto {

    /**
     * 附件文件名
     */
    private String fileName;


    /**
     * 附件路径(绝对路径)
     */
    private String filePath;
}

 

 

 

邮件发送实体

MailDto.java

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@Accessors(chain = true)
public class MailDto {

    /**
     * 主题
     */
    private String subject;

    /**
     * 邮件内容
     */
    private String text;

    /**
     * 收件人邮箱地址(发送给谁)
     */
    private String to;

    /**
     * 发送人(谁发的)
     */
    private String from;

    /**
     * 附件列表
     */
    private List<MailAttachmentDto> attachmentDtoList;
}

 

 

 

发送邮件工具类

MailUtis.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Component
public class MailUtil {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    public static MailUtil mailUtil;

    @PostConstruct
    public void init() {
        mailUtil = this;
        mailUtil.javaMailSender = this.javaMailSender;
    }


    /**
     * 发送邮件(含有附件)
     */
    public static void sendMail(MailDto mailDto) throws MessagingException {
        MimeMessage mimeMessage = mailUtil.javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject(mailDto.getSubject());
        helper.setText(mailDto.getText());
        //发送含有html代码的内容
        //helper.setText(mailDto.getText(), true);
        helper.setTo(mailDto.getTo());
        helper.setFrom(mailDto.getFrom());

        if (!CollectionUtils.isEmpty(mailDto.getAttachmentDtoList())) {
            List<MailAttachmentDto> attachmentDtoList = mailDto.getAttachmentDtoList();
            for (MailAttachmentDto attachmentDto : attachmentDtoList) {

                helper.addAttachment(attachmentDto.getFileName(), new File(attachmentDto.getFilePath()));
            }
        }

        mailUtil.javaMailSender.send(mimeMessage);
    }
}

 

使用

    /**
     * 发送邮件 测试
     * @return
     */
    @PostMapping(value = "/sendMail")
    public String sendMail(){
        MailDto dto=new MailDto();
        dto.setSubject("测试邮件主题")
                .setText("我是测试邮件具体内容")
                .setTo("223@qq.com")
                .setFrom("我是发送人");

        List<MailAttachmentDto> attachmentDtoList=new LinkedList<>();
        MailAttachmentDto attachmentDto=new MailAttachmentDto();
        attachmentDto.setFileName("1.jpg")
                .setFilePath("C:\\Users\\Pictures\\1.jpg");
        dto.setAttachmentDtoList(attachmentDtoList);
        try {
            MailUtil.sendMail(dto);
            return "success";
        } catch (MessagingException e) {
            e.printStackTrace();
           return "error";
        }

    }

 

posted @ 2020-01-23 19:30  yvioo  阅读(367)  评论(0编辑  收藏  举报