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密码,后来才知道要填写邮箱客户端授权码才行。所以打开自己的邮箱生成相应的授权码进行填写,问题就能解决了。

下面我说一下如何生成授权码:
Java发送邮箱工具类出错 :javax.mail.AuthenticationFailedException: 535 Login Fail(已解决)_第1张图片
Java发送邮箱工具类出错 :javax.mail.AuthenticationFailedException: 535 Login Fail(已解决)_第2张图片

测试结果: 可以自己给自己发邮箱
Java发送邮箱工具类出错 :javax.mail.AuthenticationFailedException: 535 Login Fail(已解决)_第3张图片

好了,继续整合项目去了,希望能帮到你!

你可能感兴趣的:(java基础学习)