【邮件发送】

邮件发送

private static final String USER = ""; // 发件人称号,同邮箱地址
    private static final String PASSWORD = "";
     @Test
    public void m5() {
        StringBuilder content = new StringBuilder("<html><head><meta charset='utf-8'></head><body>" + 111 + "" +
                ",您好:<br>您有新的岗位人员需要验收,具体情况如下:<br>员工:" +1 + "<br>线体:" +1+ "<br>岗位:" + 1 );
        content.append("<table border=\"5\" style=\"border:solid 2px #E8F2F9;font-size=14px;;font-size:25px;\">");
        content.append("<tr style=\"background-color: #428BCA; color:#ffffff\"><th>姓名</th><th>线体</th><th>岗位</th><th>是否首次操作该岗位</th><th>培训方式</th><th>打卡日期</th><th>验收状态</th></tr>");
        for (int i = 0; i < 1; i++) {
            content.append("<tr style=\"font-size:20px;\">");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("<td style=\"text-align: center\">1</td>");
            content.append("</tr>");
        }
        content.append("</table>");
        content.append("</body></html>");
        sendMail("发件人邮箱", content.toString(), "测试用");
        System.out.println("发送成功");
    }
     public static boolean sendMail (String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "SMTP");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.host", "IP地址/域名");

            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);

            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值