记录javaMailSender.createMimeMessage发送邮件

`

@Override
public Result<String> sendMailMessage(@RequestBody EntryEmpBase entryEmpBase) {
    // 1.基本信息校验
    String email = "";
    String empName;
    String sexName;
    String id;
    try {
        if (StringUtils.isNotBlank(entryEmpBase.getId())) {
            id = entryEmpBase.getId();
            EntryEmpBase entryBase = entryEmpBaseService.getById(entryEmpBase.getId());
            if (entryBase != null) {
                if (StringUtils.isNotBlank(entryBase.getEmail())) {
                    email = entryBase.getEmail();
                } else {
                    return Result.error("私人邮箱为空");
                }
                if (StringUtils.isNotBlank(entryBase.getEmpName())) {
                    empName = entryBase.getEmpName();
                } else {
                    return Result.error("姓名为空");
                }
                if (entryBase.getSexId() != null) {
                    if (entryBase.getSexId() == 1) {
                        sexName = "先生";
                    } else if (entryBase.getSexId() == 2) {
                        sexName = "女士";
                    } else {
                        return Result.error("性别有误");
                    }
                } else {
                    return Result.error("性别为空");
                }
            } else {
                return Result.error("未找到对应数据");
            }
        } else {
            return Result.error("员工id为空");
        }

        // 2.开始发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        // 邮件标题
        helper.setSubject("来自XX有限责任公司及其子公司的入职邀请");
        // 发送方地址。setFrom的邮箱地址,必须对应application.properties里面的spring.mail.username
        helper.setFrom("XX.com");
        // 接收方地址,基本信息中的人员邮件
        helper.setTo(email);
        // 发送日期
        helper.setSentDate(new Date());

        // 正文
        String content = "<div style=\"font-size: 14px;font-family:'Microsoft YaHei UI',serif;line-height:1.5;margin-left:20px\">\n" +
                "    <div style=\"white-space: pre-wrap;\">" +
                "<span>" + "亲爱的新员工: " + empName + sexName + "," + "</span></div>"
                + "    <div style=\"text-indent: 1em\">\n" +
                "      <div style=\"white-space: pre-wrap;\">恭喜您通过XX有限责任公司及其子公司的面试,即将加入XX大家庭!</div>\n" +
                "      <div>请您点击以下链接进行入职办理,操作步骤及使用方式如下:</div>\n" +
                "      <div>1、请输入手机号后四位进行登录,填写个人信息、上传入职资料,完成预入职办理。</div>\n" +
                "      <div>2、系统链接地址:(请使用谷歌或edge浏览器最新版本打开链接)</div>\n" +
                "      <div style=\"text-indent: 2em\">"
                + "<div>PC端:<a href=\"https://infosys.dgtmeta.com/entry?id=" + id + "\">https://infosys.dgtmeta.com/entry?id=" + id + "</a></div>\n"
                + "<div>手机端:<a href=\"https://jy-m.dgtmeta.com/#/pages/login/loginEntry?id=" + id + "\">https://jy-m.dgtmeta.com/#/pages/login/loginEntry?id=" + id + "</a></div>\n"
                + "</div>\n" +
                "      <div style=\"font-weight: bold\">请您在入职日期前的三日内,按照以上步骤,完成预入职办理。</div>\n" +
                "      <div style=\"font-weight: bold\"学习资料包,请在入职日期前完成学习。</div>\n" +
                "      <div style=\"font-weight: bold\">期待您的加入,谢谢配合!</div>\n" +
                "    </div>";

        String text = "<br/>\n" +
                "    <div style=\"float:right;text-align:center;margin-right: 300px\"><span>XX有限责任公司</span></div><br/>\n" +
                "    <div style=\"float:right;text-align:center;margin-right: 300px\"><span>人力资源</span></div>";

        // 图片
        helper.setText(content + "<img src='cid:file' style='width: 900px;aspect-ratio: auto 900 / 636;height: 636px;'/>" + text, true);
        String filePath = System.getProperty("user.dir") + "/img_jinsai.jpg";
        helper.addInline("file", new File(filePath));

        // 附件
        String filePath1 = System.getProperty("user.dir") + "/entry_attach01.jpg";
        String filePath2 = System.getProperty("user.dir") + "/entry_attach02.pdf";
        String filePath3 = System.getProperty("user.dir") + "/entry_attach03.xlsx";
        String filePath4 = System.getProperty("user.dir") + "/entry_attach04.pdf";
        String filePath5 = System.getProperty("user.dir") + "/entry_attach05.zip";

        helper.addAttachment("附件1-市医保卡办理指南.jpg", new File(filePath1));
        helper.addAttachment("附件2-市公积金转移说明.pdf", new File(filePath2));
        helper.addAttachment("附件3-转入XX公积金申请表(外地参保无需填写).xlsx", new File(filePath3));
        helper.addAttachment("附件4-《员工手册》-2022版.pdf", new File(filePath4));
        helper.addAttachment("附件5-学习资料包.zip", new File(filePath5));

        javaMailSender.send(mimeMessage);

        entryEmpBase.setEmail(email);
        // 入职邮件发送成功后保存到日志
        saveEmailLog(entryEmpBase, 1, null);

        return Result.OK("发送办理入职邮件成功");
    } catch (Exception e) {
        e.printStackTrace();

        entryEmpBase.setEmail(email);
        // 入职邮件发送失败后保存日志
        saveEmailLog(entryEmpBase, 0, "入职: " + e);

        return Result.error("发送办理入职邮件失败");
    }

}

`

posted @   Charles博客  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示