JavaMail抛出javax.mail.AuthenticationFailedException异常

chenjiaxi123 2009-05-14 12:22:33
package com.woaiwojia.shopping.reports;



import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;
public class Test_Javamail
{
// 21-30行把本程序所用变量进行定义。 具体在main中对它们赋植。
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = true; // smtp是否需要认证
private String username = ""; // smtp认证用户名和密码
private String password = "";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成//MimeMessage对象
public Test_Javamail(String smtp)
{
setSmtpHost(smtp);
createMimeMessage();
}

public void setSmtpHost(String hostName)
{
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}

public boolean createMimeMessage()
{
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
}
catch (Exception e)
{
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart(); // mp 一个multipart对象
// Multipart is a container that holds multiple body parts.
return true;
}
catch (Exception e)
{
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}

public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}

public void setNamePass(String name, String pass)
{
System.out.println("程序得到用户名与密码");
System.out.println("用户名:"+name);
System.out.println("密码:"+pass);
username = name;
password = pass;
}

public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
}
catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}

public boolean setBody(String mailBody)
{
try{
System.out.println("设置邮件体格式");
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+ mailBody, "text/html;charset=GB2312");mp.addBodyPart(bp);
return true;
}
catch (Exception e)
{
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}


public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
}
catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
}



public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
}
catch (Exception e)
{
return false;
}
}



public boolean setTo(String to)
{
System.out.println("设置收信人");
if (to == null)
return false;
try
{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
}
catch (Exception e)
{
return false;
}
}



public boolean setCopyTo(String copyto)
{
System.out.println("发送附件到");
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto));
return true;
}
catch (Exception e)
{
return false;
}
}



public boolean sendout()
{
try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, new auth());
Transport transport = mailSession.getTransport("smtp"); // ???
transport.connect((String) props.get("mail.smtp.host"), username,password);
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
// transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
}
catch (Exception e)
{
System.out.println("邮件发送失败!" + e);
e.printStackTrace();
return false;
}
}

public class auth extends Authenticator{
//Authenticator中的唯一一个方法,作用是返回用来认证的用户名和密码
public PasswordAuthentication getPasswordAuthentication() {
//此处的username,password对应你163邮箱的账号名称和密码
return new PasswordAuthentication(username, password);
}
}


public static void main(String[] args)
{
String mailbody = "http://www.laabc.com 用户邮件注册测试 <font color=red>欢迎光临</font> <a href=\"http://www.laabc.com\">啦ABC</a>";
Test_Javamail themail = new Test_Javamail("smtp.163.com");
themail.setNeedAuth(true);
if (themail.setSubject("laabc.com邮件测试") == false)
return;
//邮件内容 支持html 如 <font color=red>欢迎光临</font> <a href=\"http://www.laabc.com\">啦ABC</a>
if (themail.setBody(mailbody) == false)
return;
//收件人邮箱
if (themail.setTo("收件人地址") == false)
return;
//发件人邮箱
if (themail.setFrom("发件人地址") == false)
return;
//设置附件
//if (themail.addFileAffix("#######") == false)
//return; // 附件在本地机子上的绝对路径


themail.setNamePass("发件人地址", "发件人邮箱密码"); // 用户名与密码
if (themail.sendout() == false)
return;
}
}
上面这个程序无法发送,邮件地址都填写正确了,用163的,但是就是发不出去,抛出javax.mail.AuthenticationFailedException异常,试了很多次了,
...全文
56172 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
陌隋 2012-10-10
  • 打赏
  • 举报
回复
要么是账号密码操作或者是你的邮箱没有开启smtp服务。开启之后就OK了
endual 2012-08-05
  • 打赏
  • 举报
回复
可能是邮件服务器出现问题,又或者如大家说的那个用户名和密码错误。
我的问题是邮件服务器地址写错误了,我用的企业邮箱:
smtp.163.com ======>>> smtp.ym.163.com,同样的报错
emilynuannuan 2011-12-13
  • 打赏
  • 举报
回复
出了同样的问题,zghkevin的意思没看懂呢,能否讲解下?
love_killa 2011-09-23
  • 打赏
  • 举报
回复
灰常感谢 to:zghkevin!!! 你太犀利了。。。 膜拜你!
海会圣贤 2010-04-13
  • 打赏
  • 举报
回复
。。。
zghkevin 2010-04-13
  • 打赏
  • 举报
回复
javax.mail.AuthenticationFailedException 我开始也报这个ex
但我的原因是
public PasswordAuthentication getPasswordAuthentication()
我改了方法名,后来进去Authenticator,发现要重写的,不能改名
zghkevin 2010-04-13
  • 打赏
  • 举报
回复
我的可以发,我用163邮箱和qq邮箱都测试过了,可以发
package com.myjmail;

import java.io.FileInputStream;
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 MySendEmail {
public static void main(String[] args) {
try{
String userName="myEmail@163.com";
String password="*******";
String smtp_server="smtp.163.com";
String from_mail_address=userName;
String to_mail_address="yourEmail@163.com";
Authenticator auth=new PopupAuthenticator(userName,password);
Properties mailProps=new Properties();
mailProps.put("mail.smtp.host", smtp_server);
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", userName);
mailProps.put("password", password);

Session mailSession=Session.getDefaultInstance(mailProps, auth);
mailSession.setDebug(true);
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 Testw");

MimeMultipart multi=new MimeMultipart();
BodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText("电子邮件测试内容w");
//textBodyPart.setFileName("37af4739a11fc9d6b311c712.jpg");

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 pwd){
this.username=username;
this.password=pwd;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(this.username,this.password);
}
}
rockhb 2010-04-08
  • 打赏
  • 举报
回复
确实是 用户名 或者 密码错误,我也是这个问题,检查你的 邮件账号和密码。
zzzfff2008 2009-10-19
  • 打赏
  • 举报
回复
换个服务器试下吧
liguangwen86 2009-08-27
  • 打赏
  • 举报
回复
不支持smtp协议,在多少年以前注册的126和163的都支持,现在注册的都不支持smtp和pop3协议
angelduck 2009-07-05
  • 打赏
  • 举报
回复
你的好了没啊!告诉我什么原因,噢也一样
luffyke 2009-05-14
  • 打赏
  • 举报
回复
身份验证异常
在用户名和密码都没有问题的情况下,如果代码抛出javax.mail.AuthenticationFailedException异常,先用OutLook测试一下看能否进行正常的收发邮件,有时信箱如果是新注册的话,邮件的服务商默认刚注册的帐号是不能使用pop3的。例如163.com在我进行代码测试时,刚刚注册的帐号是没有权限使用该功能的,所以就会抛出上述的异常。  
还有,要注意from的email地址和Authenticator类中验证的用户名是一致的,要不也会出错.
qiheia 2009-05-14
  • 打赏
  • 举报
回复
身份验证异常。。。

请再次确认你的邮件地址是否写正确了。。。。。

81,095

社区成员

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

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