当我尝试使用JavaMail API发送邮件时收到此错误。我确信用户名和密码是100%正确的。我正在连接的Gmail帐户是一个旧帐户,因为他们说它需要时间来处理新帐户。
DEBUG SMTP RCVD:535-5.7.1不接受用户名和密码。了解更多信息
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 x35sm3011668
wfh.6
javax.mail.SendFailedException:发送失败;
嵌套异常是:
javax.mail.AuthenticationFailedException
在javax.mail.Transport.send0(Transport.java:218)
在javax.mail.Transport.send(Transport.java:80)
在Main。(Main.java:41)
在Main.main(Main.java:51)
这是我的代码:
import javax.mail。*;
import javax.mail.internet。*;
import java.util。*;
公共课主
{
String d_email =“abc@gmail.com”,
d_password =“通过”,
d_host =“smtp.gmail.com”,
d_port =“465”,
m_to =“abc@gmail.com”,
m_subject =“测试”,
m_text =“测试电子邮件。”;
公共主要()
{
属性props = new Properties();
props.put(“mail.smtp.user”,d_email);
props.put(“mail.smtp.host”,d_host);
props.put(“mail.smtp.port”,d_port);
props.put( “mail.smtp.starttls.enable”, “真”);
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.debug”,“true”);
props.put(“mail.smtp.socketFactory.port”,d_port);
props.put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”);
props.put(“mail.smtp.socketFactory.fallback”,“false”);
SecurityManager security = System.getSecurityManager();
尝试
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(真);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(m_to));
Transport.send(MSG);
}
catch(例外mex)
{
mex.printStackTrace();
}
}
public static void main(String [] args)
{
主要的blah = new Main();
}
私有类SMTPAuthenticator扩展了javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
返回新的PasswordAuthentication(d_email,d_password);
}
}
}