简体   繁体   中英

JAVA Mail - Can't send email from corporate MS office network

Following is my code to send email from the corporate MS office email id, but I am getting the error - javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful. My username and password are correct.

            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);

Try this:
props.put("mail.smtp.host", "smtp.office365.com");

Reference: Outlook mail settings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM