office365邮件发送

office365邮件发送

    //临时发送,后期对接邮件模块
    public void sendMail(String to, String subject, String content) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.partner.outlook.cn");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.transport.protocol", "smtp");
        //props.put("mail.smtp.ssl.enable", true);

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("账号","密码");
            }
        });
        session.setDebug(true);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(emailAccount);

        msg.setRecipients(Message.RecipientType.TO,to);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setContent(content, "text/html;charset=UTF-8");


        Transport.send(msg);
     }

使用

       sendMail("收件人", "标题", "内容");
评论