2014-02-15 64 views
0

该程序尝试发送电子邮件,但会引发运行时异常:AuthenticationFailedException我已经提到了stackoverflow quetion并回答了我已经实现的同样的事情,但仍然得到了像这样的异常任何人都可以解决这个问题。
异常如何解决javax.mail.AuthenticationFailedException问题

javax.mail.AuthenticationFailedException 
      at javax.mail.Service.connect(Service.java:267) 
      at javax.mail.Service.connect(Service.java:137) 
      at javax.mail.Service.connect(Service.java:86) 
      at javax.mail.Transport.send0(Transport.java:150) 
      at javax.mail.Transport.send(Transport.java:80) 
      at com.treamis.transport.vehicle.javaMail.send(javaMail.java:81) 
      at com.treamis.transport.vehicle.MysqlBackup.backupDataWithDatabase(Mysq 
    lBackup.java:97) 
      at com.treamis.transport.vehicle.MysqlBackup.run(MysqlBackup.java:118) 
      at java.util.TimerThread.mainLoop(Timer.java:555) 
      at java.util.TimerThread.run(Timer.java:505) 
    Sms sent xl sheet is generated is generated 

Java邮件代码

import java.util.Date; 
import java.util.Properties; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.Multipart; 
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 javaMail { 

    private String SMTP_PORT = "465"; 
    private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
    private String SMTP_HOST_NAME = "smtp.gmail.com"; 
    private Properties smtpProperties; 

    public javaMail() { 
     initProperties(); 
    } 

    private void initProperties() { 
     smtpProperties = new Properties(); 
     smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME); 
     smtpProperties.put("mail.smtp.auth", "true"); 
     smtpProperties.put("mail.debug", "true"); 
     smtpProperties.put("mail.smtp.port", SMTP_PORT); 
     smtpProperties.put("mail.smtp.socketFactory.port", SMTP_PORT); 
     smtpProperties.put("mail.smtp.socketFactory.class", SSL_FACTORY); 
     smtpProperties.put("mail.smtp.socketFactory.fallback", "false"); 
    } 

    public String send(String[] to, final String from, final String pwd, String subject, String body) { 
     javaMail tjm = new javaMail(); 
     try { 
      Properties props = tjm.getSmtpProperties(); 
// -- Attaching to default Session, or we could start a new one -- 
//   Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
//    protected PasswordAuthentication getPasswordAuthentication() { 
//     return new PasswordAuthentication(from, pwd); 
//    } 
//   }); 
      Session session = Session.getInstance(props, new GMailAuthenticator(from, pwd)); 
//   Session session = Session.getInstance(props, new javax.mail.Authenticator() { 
// protected PasswordAuthentication getPasswordAuthentication() { 
//  return new PasswordAuthentication(userName, password); 
// } 
//}); 
      Message msg = new MimeMessage(session); 
      MimeBodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.setText("Test mail one"); 
      Multipart multipart = new MimeMultipart(); 
      multipart.addBodyPart(messageBodyPart); 
      messageBodyPart = new MimeBodyPart(); 
      DataSource source = new FileDataSource(body); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(body); 
      multipart.addBodyPart(messageBodyPart); 
      msg.setContent(multipart); 
      msg.setFrom(new InternetAddress(from)); 
      InternetAddress[] addressTo = new InternetAddress[to.length]; 
      for (int i = 0; i < to.length; i++) { 
       addressTo[i] = new InternetAddress(to[i]); 
      } 
      msg.setRecipients(Message.RecipientType.TO, addressTo); 
//   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); 
      msg.setSubject(subject); 
      msg.setSentDate(new Date()); 
      Transport.send(msg); 
      System.out.println("Message sent OK."); 
      return "success"; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      ex.getMessage(); 
     } 
     return null; 
    } 

    public Properties getSmtpProperties() { 
     return smtpProperties; 
    } 

    public void setSmtpProperties(Properties smtpProperties) { 
     this.smtpProperties = smtpProperties; 
    } 
} 

GMailAuthenticator代码 */

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 


class GMailAuthenticator extends Authenticator { 
    String user; 
    String pw; 
    public GMailAuthenticator (String username, String password) 
    { 
     super(); 
     this.user = username; 
     this.pw = password; 
    } 
    public PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication(user, pw); 
    } 
} 
+0

嗨可以任何一个PLZ解决我的probelm – user3214269

+0

请检查您的地址和密码..并确保您的地址没有一个额外的身份验证..如移动一次性密码 – Naren

+0

我不理解你的评论 – user3214269

回答

0

AuthenticationFailedException MEA服务器认为你给了它错误的用户名或密码。你会说“当然我没有给它错误的用户名或密码,我不是那么笨!”那么,服务器不同意你。

尝试打开JavaMail session debugging,协议跟踪可能会提供有关发生了什么问题的其他线索。您可能还需要将“mail.debug.auth”属性设置为“true”以获取有关登录过程的更多详细信息。

此外,请参阅代码中某些common mistakes的JavaMail FAQ。

并确保没有拦截您尝试连接到服务器的防病毒或防火墙。