java发邮件程序一直报错javax.mail.AuthenticationFailedException

bobotaobaodian 2016-05-29 08:07:41

报错信息:
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at wx.Send.sendmail(Send.java:46)
at wx.Send.main(Send.java:61)

简单发邮件代码:
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;




public class Send {
private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private Transport transport;
private MimeMessage message;

//发送邮件
public void sendmail(final String username,final String pass,String from,String to,String subject,String content){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = System.getProperties();
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
// props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.store.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.qq.com"); //"smtp.exmail.qq.com"
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pass);
}});


message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to,false));
message.setSubject(subject);
message.setText(content);
message.setSentDate(new Date());
message.saveChanges();
Transport.send(message);
System.out.print("发送成功!");
} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();


}
}

public static void main(String[] args) throws AddressException,
MessagingException {
Send send = new Send();

send.sendmail("xx@qq.com","x", "x@qq.com","x@qq.com", "11222331","11222331");
}

}
账号密码确认是正确的,就是一直报错这个错,已在网上搜各种例子都是这样的写的,请大师们给瞅瞅。
...全文
885 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobotaobaodian 2016-05-29
  • 打赏
  • 举报
回复
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR 这个javax.mail下载的网站。
bobotaobaodian 2016-05-29
  • 打赏
  • 举报
回复
问题已经解决jar包问题,官网重新下载一个。立马好了。

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧