关于javamail问题,困惑多天了,抛出AuthenticationFailedException异常,请高手指正

hanutao 2008-10-11 07:45:15
抛出的异常:
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.kevin.MailExample.main(MailExample.java:48)


代码如下:
package com.kevin;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailExample {
public static void main(String[] args) {
try {
String username = "hanutao@163.com";
String password = "*****";
String smtp_server = "smtp.163.com";
String from_mail_address = username;
String to_mail_address = "hanutao@beelink.com";

Authenticator auth = new PopupAuthenticator(username, password);

Properties mailProps = new Properties();
mailProps.put("mail.smtp.auth", "true");

mailProps.put("username", username);

mailProps.put("password", password);

mailProps.put("mail.smtp.host", smtp_server);
Session mailSession = Session.getDefaultInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from_mail_address));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(to_mail_address));
message.setSubject("Mail Test");

MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("电子邮件测试内容!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
}
catch (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}

}

class PopupAuthenticator
extends Authenticator {
private String username;
private String password;
public PopupAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
...全文
5090 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ftbrew 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 st_beret 的回复:]
这个问题。。。。 Authentication的username 不要用带@163.com啥的 直接@前面的。。。。
[/Quote]
十分感谢。我尝试修改了很多地方,没想到问题是出在这一点。谢谢您把从痛苦中解脱出来
sunyujia 2008-10-11
  • 打赏
  • 举报
回复
550 用户被锁定

由于频繁测试用户都被锁了,lz怎么不看报错信息呢
我的贴子看没,很有可能你的积分根本就不够,不能smtp发邮件。

为何新申请的邮箱不能用客户端?字体: 小 中 大
  目前免费邮箱新注册的用户不支持直接开通smtp、pop3的服务,之前已开通客户端功能的老用户不受影响。如果需要使用该功能,您可以选择 VIP邮箱,另外我们也会陆续通过“积分活动”、“邮箱会员”等方式也会向有需要的用户提供该项服务,敬请关注。同时感谢您使用我们的产品!

http://help.163.com/07/0524/10/3F8K8I6V007525G0.html
hanutao 2008-10-11
  • 打赏
  • 举报
回复
to st_beret:
加了mailSession.setDebug(true); 后调试的结果如下:


DEBUG: setDebug: JavaMail version 1.3.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
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[071018])
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO sp8sz7lj9pybblr
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
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 "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 dXNlcm5hbWU6
aGFudXRhbw==
334 UGFzc3dvcmQ6
c29waGlh
邮件发送失败的原因是:null
具体错误原因:
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.kevin.MailExample.main(MailExample.java:49)
550 用户被锁定
sunyujia 2008-10-11
  • 打赏
  • 举报
回复
我的这篇文章上过首页经过很多网友测试了应没问题呵呵
http://blog.csdn.net/sunyujia/archive/2008/06/10/2528696.aspx

告诉你一件很重要的事情,不是所以的邮件服务商都让你用程序发邮件的
163的用户必须积分够才行,呵呵
发邮件使用smtp去
你用的邮件网站仔细看说明就知道支不支持了
foxmail和outlook可以发邮件的话就证明你的帐户可以通过写程序发邮件。
st_beret 2008-10-11
  • 打赏
  • 举报
回复
啊 我运行没错啊 。。。
要不你 加一句 mailSession.setDebug(true); 看看 debug
hanutao 2008-10-11
  • 打赏
  • 举报
回复
to st_beret:
谢谢你的回答;
请问能不能帖一份你调通的用javamail发送邮件的代码,我从网上搜到的代码基本上都报这个错。
hanutao 2008-10-11
  • 打赏
  • 举报
回复
to st_beret:
改过来了,还是报那个错
st_beret 2008-10-11
  • 打赏
  • 举报
回复
哦 顺便说一句 mail from 还是要 XXX@XXX的 邮件地址的 而且要和username一样 其实就是个验证机制
st_beret 2008-10-11
  • 打赏
  • 举报
回复
这个问题。。。。 Authentication的username 不要用带@163.com啥的 直接@前面的。。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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