2019-11-16 Java项目实战-邮件的发送

项目中邮件发送,支持企业邮件和个人邮件,仅提供spring-boot项目集成mail项目

package com.sf.vsolution.hb.sfce.util.mail;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @description: 邮件发送工具类
 * @author: zhucj
 * @date: 2019-11-06 13:29
 */
@Component
@Slf4j
public class MailSendUtil {

    @Value("${spring.mail.username}")
    String userName;

    @Autowired
    private JavaMailSender mailSender;


    /**
     * 发送普通文本邮件
     *  to收件人
     *  subject 主题
     *  content 内容
     */
    public Boolean sendSimpleMail(SendMailDto sendMailDto){
        log.info("发送邮件信息:{}", JSON.toJSONString(sendMailDto));
        SimpleMailMessage message = new SimpleMailMessage();
        //收信人
        message.setTo(sendMailDto.getTo());
        //主题
        message.setSubject(sendMailDto.getSubject());
        //内容
        message.setText(sendMailDto.getContent());
        //发信人
        message.setFrom(userName);
        mailSender.send(message);
        return true;
    }

    /**
     * 发送HTML邮件
     * @param sendMailDto
     *  to收件人
     *  subject 主题
     *  content 内容
     */
    public Boolean sendHtmlMail(SendMailDto sendMailDto){
        //使用MimeMessage,MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        //MimeMessageHelper帮助我们设置更丰富的内容
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(userName);
            helper.setTo(sendMailDto.getTo());
            helper.setSubject(sendMailDto.getSubject());
            //true代表支持html
            helper.setText(sendMailDto.getContent(), true);
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            log.error("发送HTML邮件失败: ", e);
            return false;
        }
    }

    /**
     * 发送带附件的邮件
     * @param sendMailDto
     *  to收件人
     *  subject 主题
     *  content 内容
     * @param filePath 附件路径
     */
    public Boolean sendAttachmentMail(SendMailDto sendMailDto, String filePath){
        log.info("发送邮件信息:{},附件路径:{}", JSON.toJSONString(sendMailDto),filePath);
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(userName);
            helper.setTo(sendMailDto.getTo());
            helper.setSubject(sendMailDto.getSubject());
            helper.setText(sendMailDto.getContent(), true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName.substring(1,fileName.length()-1), file);
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            log.error("发送附件的邮件失败: ", e);
            return false;
        }
    }


    /**
     * 图片邮件
     * @param sendMailDto
     * to 接收者邮件
     * subject 邮件主题
     * contnet HTML内容
     * @param rscPath 图片路径
     * @param rscId 图片ID
     * @throws MessagingException
     */
    public Boolean sendInlinkResourceMail(SendMailDto sendMailDto,
                                       String rscPath, String rscId) {
        log.info("发送邮件信息: {},发送图片路径:{},图片ID:{}", JSON.toJSONString(sendMailDto), rscPath, rscId);

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setTo(sendMailDto.getTo());
            helper.setSubject(sendMailDto.getSubject());
            helper.setText(sendMailDto.getContent(), true);
            helper.setFrom(userName);

            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            mailSender.send(message);
            return true;

        } catch (MessagingException e) {
            log.error("发送静态邮件失败: ", e);
            return false;
        }

    }

}
package com.sf.vsolution.hb.sfce.util.mail;

import lombok.*;

/**
 * @description: 发送邮件参数
 * @author: zhucj
 * @date: 2019-11-06 14:03
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class SendMailDto {

    /**
     * 收件人
     */
    private String to;

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

    /**
     * 内容
     */
    private String content;
}
spring:    
    mail:
      #发件人邮箱
      username: ****
      #发件人密码或授权码(企业邮件设置成密码,QQ邮箱为授权码)
      password: ****
      #发件服务器(客户端设置中查看)
      host: ****
      default-encoding: UTF-8
      #发件服务器端口(客户端设置中查看)
      port: ****
      protocol: smtp
      #收件人邮箱
      toMailName:
        #邮箱一
        one: ******
 <!--发送邮箱-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

猜你喜欢

转载自www.cnblogs.com/zhucj-java/p/11868673.html
今日推荐