javax.mail.AuthenticationFailedException Java Mail

时间:2015-08-31 04:14:16

标签: java email javax.mail

我制作了一个小程序,可以向我的电子邮件帐户发送消息。我的大多数代码似乎都运行正常。但是,当我的程序中的代码命中transport.send时,我遇到了AuthenticationFailedException。我不确定为什么,因为我认为我已经正确设置了所有内容。这是我的代码。

JavaEmail.java

public static void main (String[] args) throws Exception{
                //intialize logger 
protected static Logger logger = LogManager.getLogger(JavaEmail.class.getName());

//smtp related parameters 
private static String smtpUseremail;
private static String smtpReceiverEmailAddress;
private static String smtpUserpassword;
private static String smtpPortnumber;
private static String smtpHost;

private static String emailSubject;

//stack error message variable 
private static String message;

        try {

            logger.info("-----------------------------");
            logger.info("---------Starting up---------");
            logger.info("-----------------------------");

            initialize();

            String body = "This is a test";

            logger.info("Start to connect to email server");

            sendFromGmail(smtpUseremail, smtpUserpassword, smtpReceiverEmailAddress, emailSubject, body);



           // System.out.println("error message: " + message);

            logger.info("Email Sent");


        }
        catch (Exception e){
            logger.error(e);

            // message = e.toString();

            throw e;


        }
    }

    //Setting up Email Server
    private static void sendFromGmail(String from, String pass, String to, String subject, String body) {


        Properties props = PropertiesUtil.loadProperties("main.properties");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPortnumber);

        Session session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(smtpUseremail, smtpUserpassword);
                    }
                  });

        MimeMessage message = new MimeMessage(session);



        try{


            message.setFrom(new InternetAddress(from));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));


            message.setSubject(subject);

            message.setText(body);

            Transport.send(message);

            logger.info("Message has been delievered, check your mail");

        }

        catch (MessagingException e) {
            logger.error("Messaging Exception");

            throw new RuntimeException(e);
        }
    }


    //Initializing Properties
    private static void initialize() throws Exception  {

        Properties props = PropertiesUtil.loadProperties("main.properties");

        //smtp set up
        smtpPortnumber = props.getProperty("portNumber");
        smtpReceiverEmailAddress = props.getProperty("recipientEmailAddress");
        smtpUseremail = props.getProperty("userEmailAddress");
        smtpUserpassword = props.getProperty("userPassword");
        smtpHost = props.getProperty("emailHost");
        emailSubject = props.getProperty("");

    }

}

main.properties

portNumber = 465
recipientEmailAddress = my@gmail.com
userPassword = secert
userEmailAddress = my@gmail.com
emailHost = smtp.gmail.com

错误

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
    534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
    534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
    534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
    534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
    534-5.7.14 then try again.
    534-5.7.14  Learn more at
    534 5.7.14  https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp

        at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:140)
        at emailNotification.JavaEmail.main(JavaEmail.java:63)
    Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
    534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
    534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
    534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
    534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
    534-5.7.14 then try again.
    534-5.7.14  Learn more at
    534 5.7.14  https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp

        at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:130)
        ... 1 more

2 个答案:

答案 0 :(得分:17)

转到浏览器然后登录您在代码中尝试的Gmail帐户,然后转到此链接https://www.google.com/settings/security/lesssecureapps

你会看到

enter image description here

您应该点击TURN ON

答案 1 :(得分:0)

我已解决了激活DisplayUnlockCaptcha的问题:

https://accounts.google.com/b/0/DisplayUnlockCaptcha

还激活了Lesssecureapps

https://www.google.com/settings/security/lesssecureapps