email工具

package com.sdj.cashier.agent.center.common.utils;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Copyright (C), 
 *
 * @Description: email工具
 * @ClassName: MailUtil
 * @Package com.common.utils
 * @Author: hanning
 * @Date: 2022/1/14 下午2:26
 * CREATE WITH IntelliJ IDEA
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public class MailUtil {

    private static JavaMailSenderImpl javaMailSender;
	//	发件人
    private static final String SENDER = "xxx@126.com";

    static {
        javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.126.com");// 链接服务器
//        javaMailSender.setPort(25);// 默认使用25端口发送
        javaMailSender.setUsername("xxx@126.com");// 邮箱账号
//        javaMailSender.setPassword("VMJKKSSQJQHZWPHM");// 授权码
        javaMailSender.setPassword("xxx");// 授权码
        javaMailSender.setDefaultEncoding("UTF-8");
//        javaMailSender.setProtocol("smtp");

//        Properties properties = new Properties();
//        properties.setProperty("mail.debug", "true");// 启用调试
//        properties.setProperty("mail.smtp.timeout", "1000");// 设置链接超时
//        设置通过ssl协议使用465端口发送、使用默认端口(25)时下面三行不需要
//        properties.setProperty("mail.smtp.auth", "true");// 开启认证
//        properties.setProperty("mail.smtp.socketFactory.port", "465");// 设置ssl端口
//        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

//        javaMailSender.setJavaMailProperties(properties);
    }

    public static void main(String[] args) throws Exception {
//        sendSimpleMail(new String[]{"xxx@qq.com"}, "邮件主题", "邮件内容", false);
        File file = new File("xx../file/xxx.docx");
        Map map = new HashMap();
        map.put(file.getName(),file);
        sendAttachmentMail(new String[]{"xxx@qq.com"}, "邮件主题", "邮件内容", map);
    }

    /**
     * 发送普通邮件
     *
     * @param to 收件人
     * @param subject 主题
     * @param text 正文
     * @param isHtml 正文是否为html格式
     */
    public static void sendSimpleMail(String[] to, String subject, String text, boolean isHtml) throws Exception {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(SENDER, "通知");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, isHtml);
        javaMailSender.send(message);
    }

    /**
     * 发送带附件邮件
     *
     * @param to 收件人
     * @param subject 主题
     * @param text 正文
     * @param files 附件
     */
    public static void sendAttachmentMail(String[] to, String subject, String text, Map<String, File> files) throws Exception {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(SENDER, "通知");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        Set<Map.Entry<String, File>> fileSet = files.entrySet();
        for (Map.Entry f : fileSet) {
            helper.addAttachment((String) f.getKey(), (File) f.getValue());
        }
        javaMailSender.send(message);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值