Java發送郵箱工具類出錯 :javax.mail.AuthenticationFailedException: 535 Login Fail(已解決)

今天在項目整合郵件發送工具類時出了個小錯,如下:

D:\java1.8\jdk\bin\java.exe "-javaagent:F:\software\Software\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=55937:F:\software\Software\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\java1.8\jdk\jre\lib\charsets.jar;D:\java1.8\jdk\jre\lib\deploy.jar;D:\java1.8\jdk\jre\lib\ext\access-bridge-64.jar;D:\java1.8\jdk\jre\lib\ext\cldrdata.jar;D:\java1.8\jdk\jre\lib\ext\dnsns.jar;D:\java1.8\jdk\jre\lib\ext\jaccess.jar;D:\java1.8\jdk\jre\lib\ext\jfxrt.jar;D:\java1.8\jdk\jre\lib\ext\localedata.jar;D:\java1.8\jdk\jre\lib\ext\nashorn.jar;D:\java1.8\jdk\jre\lib\ext\sunec.jar;D:\java1.8\jdk\jre\lib\ext\sunjce_provider.jar;D:\java1.8\jdk\jre\lib\ext\sunmscapi.jar;D:\java1.8\jdk\jre\lib\ext\sunpkcs11.jar;D:\java1.8\jdk\jre\lib\ext\zipfs.jar;D:\java1.8\jdk\jre\lib\javaws.jar;D:\java1.8\jdk\jre\lib\jce.jar;D:\java1.8\jdk\jre\lib\jfr.jar;D:\java1.8\jdk\jre\lib\jfxswt.jar;D:\java1.8\jdk\jre\lib\jsse.jar;D:\java1.8\jdk\jre\lib\management-agent.jar;D:\java1.8\jdk\jre\lib\plugin.jar;D:\java1.8\jdk\jre\lib\resources.jar;D:\java1.8\jdk\jre\lib\rt.jar;E:\skills\項目\travel\travel\target\classes;F:\software\Software\maven\LocalWarehouse\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar;F:\software\Software\maven\LocalWarehouse\com\alibaba\druid\1.0.9\druid-1.0.9.jar;D:\java1.8\jdk\lib\jconsole.jar;D:\java1.8\jdk\lib\tools.jar;F:\software\Software\maven\LocalWarehouse\org\springframework\spring-core\4.1.2.RELEASE\spring-core-4.1.2.RELEASE.jar;F:\software\Software\maven\LocalWarehouse\org\springframework\spring-jdbc\4.1.2.RELEASE\spring-jdbc-4.1.2.RELEASE.jar;F:\software\Software\maven\LocalWarehouse\org\springframework\spring-tx\4.1.2.RELEASE\spring-tx-4.1.2.RELEASE.jar;F:\software\Software\maven\LocalWarehouse\org\springframework\spring-beans\4.1.2.RELEASE\spring-beans-4.1.2.RELEASE.jar;F:\software\Software\maven\LocalWarehouse\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;F:\software\Software\maven\LocalWarehouse\commons-beanutils\commons-beanutils\1.9.2\commons-beanutils-1.9.2.jar;F:\software\Software\maven\LocalWarehouse\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;F:\software\Software\maven\LocalWarehouse\com\fasterxml\jackson\core\jackson-databind\2.3.3\jackson-databind-2.3.3.jar;F:\software\Software\maven\LocalWarehouse\com\fasterxml\jackson\core\jackson-core\2.3.3\jackson-core-2.3.3.jar;F:\software\Software\maven\LocalWarehouse\com\fasterxml\jackson\core\jackson-annotations\2.3.3\jackson-annotations-2.3.3.jar;F:\software\Software\maven\LocalWarehouse\javax\mail\javax.mail-api\1.5.6\javax.mail-api-1.5.6.jar;F:\software\Software\maven\LocalWarehouse\com\sun\mail\javax.mail\1.5.3\javax.mail-1.5.3.jar;F:\software\Software\maven\LocalWarehouse\javax\activation\activation\1.1\activation-1.1.jar;F:\software\Software\maven\LocalWarehouse\redis\clients\jedis\2.7.0\jedis-2.7.0.jar;F:\software\Software\maven\LocalWarehouse\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar com.xiaojie.travel.util.MailUtils
javax.mail.AuthenticationFailedException: 535 Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
	at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
	at javax.mail.Service.connect(Service.java:388)
	at javax.mail.Service.connect(Service.java:246)
	at javax.mail.Service.connect(Service.java:195)
	at javax.mail.Transport.send0(Transport.java:254)
	at javax.mail.Transport.send(Transport.java:124)
	at com.xiaojie.travel.util.MailUtils.sendMail(MailUtils.java:62)
	at com.xiaojie.travel.util.MailUtils.main(MailUtils.java:71)
Process finished with exit code 0

工具類:

package com.xiaojie.travel.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 發郵件工具類
 */
public final class MailUtils {
    private static final String USER = "[email protected]"; // 發件人郵箱地址
    private static final String PASSWORD = "******"; // 客戶端授權碼
    /**
     *
     * @param to 收件人郵箱
     * @param text 郵件正文
     * @param title 標題
     */
    /* 發送驗證信息的郵件 */
    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");

            // 發件人的賬號
            props.put("mail.user", USER);
            //發件人的密碼
            props.put("mail.password", PASSWORD);

            // 構建授權信息,用於進行SMTP進行身份驗證
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用戶名、密碼
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用環境屬性和授權信息,創建郵件會話
            Session mailSession = Session.getInstance(props, authenticator);
            // 創建郵件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 設置發件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 設置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 設置郵件標題
            message.setSubject(title);

            // 設置郵件的內容體
            message.setContent(text, "text/html;charset=UTF-8");
            // 發送郵件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws Exception { // 做測試用
        MailUtils.sendMail("[email protected]","你好,這是一封測試郵件,無需回覆。","測試郵件");
        System.out.println("發送成功");
    }



}

一開始我是在工具類中:PASSWORD 填的qq密碼,後來才知道要填寫郵箱客戶端授權碼才行。所以打開自己的郵箱生成相應的授權碼進行填寫,問題就能解決了。

下面我說一下如何生成授權碼:
在這裏插入圖片描述
在這裏插入圖片描述

測試結果: 可以自己給自己發郵箱
在這裏插入圖片描述

好了,繼續整合項目去了,希望能幫到你!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章