当前位置: 代码迷 >> J2EE >> 用javamail发送邮件时,为什么会发生证明会失败(authentication failed),该如何解决
  详细解决方案

用javamail发送邮件时,为什么会发生证明会失败(authentication failed),该如何解决

热度:127   发布时间:2016-04-22 01:48:18.0
用javamail发送邮件时,为什么会发生证明会失败(authentication failed)
public   void   sendEmail(String[]   to,   String   subject,   String   text,
String   from,   String   password)   {
              try
{
                    String   host= "smtp.163.com ";
String   username= "gq800307 ";
Properties   props   =   new   Properties();
props.put( "mail.smtp.host ",   host);
props.put( "mail.smtp.auth ",   "true ");   //   这样才能通过验证
                  Session   session   =   Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage   message   =   new   MimeMessage(session);
MimeMultipart   content   =   new   MimeMultipart();   //   创建邮件体对象
if   (content   ==   null)
            ;
MimeBodyPart   part   =   new   MimeBodyPart();//   创建文本部分对象
String   type   =   "text/plain ";//   文本类型默认为text/plain  
if   (text.startsWith( " <html> ")   ||   text.startsWith( " <HTML> "))
            type   =   "text/html ";  
//   如果是网页形式的内容,则改变类型为text/html
part.setText(text);
part.setHeader( "Content-Type ",   type);
content.addBodyPart(part);
message.setContent(content);   //   添加文本至邮件中
message.saveChanges();   //   保存修改
message.setFrom(new   InternetAddress(from));
Transport   transport   =   session.getTransport( "smtp ");

for   (int   j   =   0;   j   <   to.length;   j++)   {
System.out.println(to[j].toString()   +   "========213=== ");
message.addRecipient(Message.RecipientType.TO,
new   InternetAddress(to[j]));
message.setSentDate(new   Date());
message.setSubject(subject);
message.setText(text);
//   Send   message
message.saveChanges();
System.out.println( "==========223==== ");
transport.connect(host,   username,   password);
System.out.println( "==========225==== ");
transport.sendMessage(message,   message.getAllRecipients());
System.out.println( "j= "   +   j   +   to.toString()   +   "*************** ");
transport.close();
}

------解决方案--------------------
把错误贴出来

------解决方案--------------------
我用smtp.163.com老是出javax.mail.AuthenticationFailedException.
自己用transport.isConnect()判断连接上了,就是发生这个异常,我也重写了认证类了,代码如下:

Session session = Session.getDefaultInstance(props,new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication( "lean1252 ", "lean433957 ");
if(pa == null) {
System.out.println( "auth failed... ");
}
return pa;
}
});
可它为什么问题提示认证失败了呢?
把smtp.gmail.com就能发送,不知道是什么原因
------解决方案--------------------
PasswordAuthentication pa = new PasswordAuthentication
  相关解决方案