JAVA邮件 - 无法从公司MS办公室网络发送电子邮件

时间:2016-06-16 20:57:29

标签: java

以下是我从公司MS办公室电子邮件ID发送电子邮件的代码,但我收到错误 - javax.mail.AuthenticationFailedException:535 5.7.3身份验证失败。我的用户名和密码是正确的。

            final String user="abc@abc.com";
            final String password = "1234";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            //props.put("mail.smtp.host", "smtp-mail.outlook.com");
            props.put("mail.smtp.host", "outlook.office365.com");
            props.put("mail.smtp.port", "587");

            Session session = Session.getInstance(props,
                      new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(user, password);
                        }
                      });
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(sendEmailAddress));

            String text = "Test email";


            message.setSubject("Test email");
            message.setText(text);
            Transport.send(message);

1 个答案:

答案 0 :(得分:0)

试试这个:
props.put("mail.smtp.host", "smtp.office365.com");

参考:Outlook mail settings