javamail验证失败Authentication failed的原因以及解决办法

javamail邮件开发:
验证失败的原因和解决办法:
发送成功之后再控制台打印的内容



1.先申请一个邮箱,然后开通pop3/smtp服务
这里我用的是网易的163邮箱
在开启POP3/SMTP服务的时候会让你输入一个授权码,这个授权码后面会用到.
javamail验证失败Authentication failed的原因以及解决办法_第1张图片

2.代码的书写:

@Test
    public void testJavaMail() throws Exception{
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.163.com");//指定邮件的发送服务器地址
        props.put("mail.smtp.auth", "true");//服务器是否要验证用户的身份信息

        Session session = Session.getInstance(props);//得到Session
        session.setDebug(true);//代表启用debug模式,可以在控制台输出smtp协议应答的过程


        //创建一个MimeMessage格式的邮件
        MimeMessage message = new MimeMessage(session);

        //设置发送者
        Address fromAddress = new InternetAddress("[email protected]");//邮件地址
        message.setFrom(fromAddress);//设置发送的邮件地址
        //设置接收者
        Address toAddress = new InternetAddress("[email protected]");//要接收邮件的邮箱
        message.setRecipient(RecipientType.TO, toAddress);//设置接收者的地址

        //设置邮件的主题
        message.setSubject("xxxx");
        //设置邮件的内容
        message.setText("xxxxxxxx");
        //保存邮件
        message.saveChanges();


        //得到发送邮件的服务器(这里用的是smtp服务器)
        Transport transport = session.getTransport("smtp");

        //发送者的账号连接到smtp服务器上  @163.com可以不写
transport.connect("smtp.163.com","[email protected]","授权码"); 
        //发送信息
        transport.sendMessage(message, message.getAllRecipients());
        //关闭服务器通道
        transport.close();
    }

3.验证失败的原因:

    //发送者的账号连接到smtp服务器上  @163.com可以不写
transport.connect("smtp.163.com","xxxx@163.com","授权码"); 

应该填的是授权码,而不是你邮箱的登陆密码,要注意

4.发送成功之后再控制台打印的内容

DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25, isSSL false
220 163.com Anti-spam GT for Coremail System (163com[20141201])
DEBUG SMTP: connected to host "smtp.163.com", port: 25

EHLO SKY-20161018HDH
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2Ur6TvxkUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2Ur6TvxkUCa0xDrUUUUj"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
AUTH LOGIN
334 dXNlcm5hbWU6
MTgzMDA2MDI0MDdAMTYzLmNvbQ==
334 UGFzc3dvcmQ6
a2tib2trMTIz
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:@163.com>
250 Mail OK
RCPT TO:.com>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxxx@qq.com
DATA
354 End data with .
From: xxxx@163.com
To: xxxx@qq.com
Message-ID: <1243355991.0.1494736360086.JavaMail.Administrator@SKY-20161018HDH>
Subject: =?UTF-8?B?5ZOl5ZOl77yM5pma5LiK57qm5L2g77yB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

5oiR5pivY2d444CC44CC44CC44CC6ICB5Zyw5Z2A6KeB77yBZGFz
.
250 Mail OK queued as smtp5,D9GowAD3C3Tn3RdZqs5eAA--.3250S2 1494736361
QUIT

你可能感兴趣的:(web开发)