javamail发送邮件报javax.mail.AuthenticationFailedException错误!

时间:2021-12-17 18:15:28

 

javamail发送邮件报javax.mail.AuthenticationFailedException错误! 需要这两个jar包的支持,下面是一个完整可用的代码,至于我为什么报这个错误的原因,就是因为输入的是qq密码,而不是邮箱独立密码,也提醒其余同学,若碰到此问题,请仔细验证自己的账号密码,以及格式问题

package com.yc.mail;

import java.util.Date;
import java.util.Vector;

public class Mail {
private String title; // 主题
private String inceptAddress; // 收件人地址
private String sendAddress; // 发件人地址
private String content; // 邮件正文
private Vector file; // 附件
private Date sendTime; // 发送时间

public Mail() {
title = "";
inceptAddress = "";
sendAddress = "";
content = "";
file = new Vector();
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Vector getFile() {
return file;
}

public void setFile(Vector file) {
this.file = file;
}

public String getInceptAddress() {
return inceptAddress;
}

public void setInceptAddress(String inceptAddress) {
this.inceptAddress = inceptAddress;
}

public String getSendAddress() {
return sendAddress;
}

public void setSendAddress(String sendAddress) {
this.sendAddress = sendAddress;
}

public Date getSendTime() {
return sendTime;
}

public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

// 该方法用于收集附件名
public void attachfile(String fname) {
file.addElement(fname);
}

}

 

package com.yc.mail;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeUtility;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import javax.mail.MessagingException;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Date;

public class MailFacade {
private static String SMTPServer;
private static String strUser;
private static String strPwd;
private static BodyPart mbp;
private static String Centent;

public MailFacade() {
SMTPServer = "smtp.qq.com";
strUser = ""; //输入发送方的 qq号 例如"449301818"
strPwd = ""; //独立密码!!
}

/**
* 发送邮件方法 接收一个Mail对象(封装邮件)
*/
public boolean startSend(Mail mail) throws Exception {
// 创建Properties对象
Properties props = System.getProperties();

// 创建信件服务器

props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");// 执行SMTP验证





MyAuthenticator auth = new MyAuthenticator(<span style="font-family: Arial, Helvetica, sans-serif;">strUser,</span><span style="font-family: Arial, Helvetica, sans-serif;">strPwd</span>);

// 得到默认的对话对象
Session session = Session.getDefaultInstance(props, auth);
try {
// 创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);

// 发送人地址
msg.setFrom(new InternetAddress(mail.getSendAddress()));

// 收件人地址
InternetAddress[] address = { new InternetAddress(mail
.getInceptAddress()) };

msg.setRecipients(Message.RecipientType.TO, address);

// 设置主题
msg.setSubject(mail.getTitle().trim());

// 后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();

// 邮件正文
BodyPart mbpText = new MimeBodyPart();
mbpText.setContent(mp, mail.getContent());

// 利用枚举器方便的遍历集合
Enumeration efile = mail.getFile().elements();

// 检查序列中是否还有更多的对象
String filename = "";
while (efile.hasMoreElements()) {
BodyPart mbp = new MimeBodyPart();
// 选择出每一个附件名
filename = efile.nextElement().toString();

// 得到数据源
FileDataSource fds = new FileDataSource(filename);
// 得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));

// 得到文件名同样至入BodyPart(同时处理中文文件名)
mbp.setFileName(MimeUtility.encodeWord(fds.getName(), "utf-8",
null));
mp.addBodyPart(mbp);
}
// 移走集合中的所有元素
mail.getFile().removeAllElements();

// Multipart加入到信件
msg.setContent(mp);

// 设置信件头的发送日期
msg.setSentDate(new Date());

// 发送信件
Transport.send(msg);

} catch (MessagingException mex) {
mex.printStackTrace();
// Exception ex = null;
// if ((ex=mex.getNextException())!=null){
// ex.printStackTrace();
// }
// return false;
}
return true; // 发送成功
}

// SMTP验证类(内部类),继承javax.mail.Authenticator
class MyAuthenticator extends Authenticator {
private String strUser;
private String strPwd;

public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}

}



 


 

package com.yc.mail;

public class Test1 {
public static void main(String args[]) throws Exception {

MailFacade mailFacade = new MailFacade();

Mail mail = new Mail();
mail.setSendAddress("449301818@qq.com"); //发送方
mail.setInceptAddress("449301818@qq.com"); //接收者
mail.setTitle("hello world");
mail.setContent("您好:这是邮件正文,谢谢!");
mail.attachfile("c:\\a.txt");
mail.attachfile("c:\\b.txt");

System.out.println(mailFacade.startSend(mail));
}

}