javaMailSender

javaMailSender


基于spring-boot 2.x:


maven:

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

interface:

public interface MailService {

    void sendMail(String subject, String body, String from, String[] to) throws Exception;

    void sendMail(String subject, String body, String from, String[] cc, String[] to) throws Exception;

    void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to) throws Exception;

    void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to, List<String> attachPathList) throws Exception;
}

impl:

@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    /**
     * 发送邮件
     *
     * @param subject 主题
     * @param body    内容
     * @param from    发件人
     * @param to      收件人[多个]
     */
    public void sendMail(String subject, String body, String from, String[] to) throws Exception {
        sendMail(subject, body, from, null, to);
    }

    /**
     * 发送邮件
     *
     * @param subject 主题
     * @param body    内容
     * @param from    发件人
     * @param cc      抄送人[多个]
     * @param to      收件人[多个]
     */
    public void sendMail(String subject, String body, String from, String[] cc, String[] to) throws Exception {
        sendMail(subject, body, false, from, cc, to);
    }

    /**
     * 发送邮件
     *
     * @param subject 主题
     * @param body    内容
     * @param html    是否为html格式
     * @param from    发件人
     * @param cc      抄送人[多个]
     * @param to      收件人[多个]
     */
    public void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to) throws Exception {
        sendMail(subject, body, html, from, cc, to, null);
    }

    /**
     * 发送邮件
     *
     * @param subject        主题
     * @param body           内容
     * @param html           是否为html格式
     * @param from           发件人
     * @param cc             抄送人[多个]
     * @param to             收件人[多个]
     * @param attachPathList 附件路径
     */
    public void sendMail(String subject, String body, boolean html, String from, String[] cc, String[] to, List<String> attachPathList) throws Exception {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setSubject(subject);
            helper.setFrom(new InternetAddress(from));
            helper.setSentDate(new Date());

            // 设置收件人地址
            helper.setTo(to);

            // 设置抄送人
            if (!StringUtils.isEmpty(cc)) {
                helper.setCc(cc);
            }

            // 设置正文
            helper.setText(body, html);

            // 设置附件
            if (!CollectionUtils.isEmpty(attachPathList)) {
                for (String attachPath : attachPathList) {
                    FileDataSource fileDataSource = new FileDataSource(attachPath);
                    helper.addAttachment(fileDataSource.getName(), fileDataSource);
                }
            }
            // 正式发送邮件
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送邮件失败:{}", e.getMessage());
            throw e;
        }
    }

config是基于application.xml/yml配置

example1:
指定465端口, 可以不填默认使用25端口

spring:
  #     邮件发送配置(改为自己的账号和密码)
  mail:
    host: xxx
    username: xxx
    password: xxx
    default-encoding: UTF-8
 	properties:
		mail:
			smtp:
 				auth: true
				starttls:
					enable: true
					required: true
				socketFactory:
					port: 465
					class:  javax.net.ssl.SSLSocketFactory
 				 	fallback: false

example2:
简单配置

spring:
  #     邮件发送配置(改为自己的账号和密码)
  mail:
    host: xxx
    port: 25
    username: xxx
    password: xxx
    default-encoding: UTF-8
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值