java mail 异常javax.mail.AuthenticationFailedException
测试类:
public class TestSendEmail {
public static void main(String[] args){
//这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("chendonghai@fndsoft.cn");
mailInfo.setPasssword("*******************");
mailInfo.setFromAddress("chendonghai@fndsoft.cn");
mailInfo.setToAddress("hepeihui@fndsoft.cn");
mailInfo.setSubject("验证用户,发送验证码");
mailInfo.setContent("woshichendonghai");
//这个类主要来发送邮件
SimpleMailSender sms = new SimpleMailSender();
sms.sendTextMail(mailInfo);
}
}
-------------------------------------------------------------------------------------------------------------------------------
邮件信息类:
上主要代码,其他都是一些内容,主题啥的
/**
* 配置邮件的会话属性
* */
public Properties getProperties(){
Properties pro = new Properties();
pro.put("mail.smtp.host", this.mailServerHost);
pro.put("mail.smtp.port", this.mailServerPort);
//pro.put("mail.smtp.starttls.enable","true");
pro.put("mail.smtp.auth", "true");
//pro.put("mail.smtp.auth", validate?"true":"false");
return pro;
}
-------------------------------------------------------------------------------------------------------------------------------
验证认证类:
public class MyAuthenticator extends Authenticator{
String userName = null;
String password = null;
public MyAuthenticator() {
}
public MyAuthenticator(String password, String userName) {
this.password = password;
this.userName = userName;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
-------------------------------------------------------------------------------------------------------------------------------
邮件发送类:
public class SimpleMailSender {
public boolean sendTextMail(MailSenderInfo mailInfo){
//判断是否需要验证身份
Properties pro = mailInfo.getProperties();
//需要身份验证,则创建一个密码验证器(认证类)
MyAuthenticator authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPasssword());
//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
//根据session创建一个邮件消息对象
Message mailMessage = new MimeMessage(sendMailSession);
//创建发送人的地址
Address from = new InternetAddress(mailInfo.getFromAddress());
//设置邮件的发送者到邮件消息对象
mailMessage.setFrom(from);
//创建接收人的地址,并设置到邮件消息对象中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
//设置邮件对象的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件的发送时间
mailMessage.setSentDate(new Date());
//设置邮件的正文
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
/*Transport transport = sendMailSession.getTransport();
transport.connect("smpt.163.com", mailInfo.getUserName(), mailInfo.getPasssword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());*/
Transport.send(mailMessage);
return true;
}catch (MessagingException ex){
ex.printStackTrace();
}
return false;
}
}
-------------------------------------------------------
其中查了原因,网上说了很多,包括新账号没有加认证等,我这里是企业邮箱,不知道问题出在哪,求解救。