DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.sina.com, user=fyf, password=<null>
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
显示登录成功但是发送需要认证(
need username and password
for
authentication
)
所以session获取方法需要修改,添加认证信息
Session session = Session.getInstance(prop);
Session session = Session.getDefaultInstance(prop,new SimpleAuthenticator(USERNAME,PASSWORD));
其中SimpleAuthenticator类需要继承javax.mail.Authenticator;
因为javax.mail.Authenticator;并没有实现getPasswordAuthentication()方法,根据Authenticator中对该方法的描述
* Called when password authentication is needed. Subclasses should
* override the default implementation, which returns null. <p>
* Note that if this method uses a dialog to prompt the user for this
* information, the dialog needs to block until the user supplies the
* information. This method can not simply return after showing the
* dialog.
* @return The PasswordAuthentication collected from the
* user, or null if none is provided.
protected PasswordAuthentication getPasswordAuthentication() {
return null;
当需要密码认证是会被调用,子类必须实现该方法,并返回PasswordAuthentication,重写代码如下
package com.taotao.common.utils;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SimpleAuthenticator extends Authenticator {
private String username;
private String password;
public SimpleAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, password);
再次运行发送成功
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<yonglai_zhuce@sina.com>
250 ok
RCPT TO:<truthmustdie@163.com>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: truthmustdie@163.com
354 End data with <CR><LF>.<CR><LF>
Date: Fri, 14 Dec 2018 20:26:37 +0800 (CST)
From: yonglai_zhuce@sina.com
To: truthmustdie@163.com