首页 > 解决方案 > AuthenticationFailedException in sending email from java code

问题描述

I am trying to write a small java program that can send email from my corporate outlook account. I am planning to run this java program on my office machine only. Below is my program:

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.port", "587");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(my_email_id, my_password);
            }
          });
        
        MimeMessage mime = new MimeMessage(session);
        
        try {
            mime.setSender(new InternetAddress(emailid));
            mime.setRecipient(Message.RecipientType.TO, new InternetAddress(emailid));
            mime.setSubject("Test");
            mime.setText("Testing");
            Transport.send(mime);
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am getting below error :

javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR01CA0166.INDPRD01.PROD.OUTLOOK.COM]

I have tried removing the socketFactory properties as well, but no luck. Can anyone help me out with this one? I am guessing it might have something to do with proxy or any other security on my office machine, but I am not able to figure out what it is.

标签: javaemailoutlookmime

解决方案


推荐阅读