Java_发送邮件

Java_发送邮件

  1. 发送邮件方法
 /**
     * 发送邮件
     * @param receive 收件人 (邮箱)
     * @param subject 邮件主题
     * @param map     模板映射内容
     * @param String  模板路径
     * @return
     * @throws GeneralSecurityException
     */
    public static boolean sendMail(String receive, String subject, Map map,String templateName)throws GeneralSecurityException {
        if (StringUtils.isEmpty(receive)) {
            return false;
        }
        // 发件人电子邮箱
        final String from = "XXX@163.com";
        // 发件人电子邮箱密码(授权码)
        final String pass = "XXX";
        // 指定邮件服务器
        String host = "smtp-n.global-mail.cn";
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        //获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, pass);
            }
        });
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
            message.setSubject(subject);
            String msg = getMailTextByTemplateName(templateName, map);
            // 发送完整消息
            message.setContent(msg,"text/html;charset=UTF-8");
            // 发送消息
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

 //获取邮件发送内容_本项目使用Httl模板引擎(具体根据项目进行替换)
 private static String getMailTextByTemplateName(String templateName, Map<String, String> params){
        String mailText = "";
        try {
        	//此方法是公司自己封装,请根据具体项目获取邮件发送内容(邮件发送内容是String类型的字符串)
             mailText = TextTemplateUtil.mergingByClassPath(templateName, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mailText;
    }
  1. 邮件模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>邮件提醒</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>

<body style="margin: 0; padding: 0;">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
    <tr>
        <td>
            <p style="font-size: 25px;color: #3f3f3f;font-weight: bold;text-align: center;">XXX技术有限公司</p>
        </td>
    </tr>

    <tr>
        <td>
            <div style="border: #36649d 1px dashed;margin: 30px;padding: 20px">
                <label style="font-size: 22px;color: #36649d;font-weight: bold"></label>
                <p style="font-size: 16px"> 您好!您申请的项目为:<strong> ${projectName} </strong></p>
                <p> 请点击以下链接获取发版文件 </p>
                <a href ="${downUrl}">${downUrl}</a>
                <p style=" font-size: 16px">( 链接48小时内有效,如无法点击,请复制链接到浏览器访问 )</p>
            </div>
        </td>
    </tr>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值