2011-07-23 29 views
6

Tôi không hiểu tại sao tôi nhận ngoại lệ này. Đây là mã tìm cách gửi tin nhắn email.javax.mail.AuthenticationFailedException: 535 5.0.0 Xác thực Không thành công

public void sendAsHotmail() { 
    final String username = jTextField14.getText(); 
    final String password = jPasswordField4.getText(); 
    String subject = jTextField16.getText(); 
    String Cc = jTextField17.getText(); 
    String Bcc = jTextField18.getText(); 
    String recipient = jTextArea5.getText(); 

    Properties props = new Properties(); 
    props.put("mail.smtp.host" , "smtp.live.com"); 
    props.put("mail.smtp.user" , username); 

    // Use TLS 
    props.put("mail.smtp.auth" , "true"); 
    props.put("mail.smtp.starttls.enable" , "true"); 
    props.put("mail.smtp.password" , password); 

    Session session = Session.getDefaultInstance(props , new Authenticator() { 
     @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        if(username == null | password == null) 
         JOptionPane.showMessageDialog(new JFrame() , "username or password incorrect"); 
        return new PasswordAuthentication(username , password); 
       } 
    }); 
    String to = recipient; 
    String from = username + "@hotmail.com"; 
    String emailMessage = jTextArea2.getText(); 
    MimeMessage message = new MimeMessage(session); 
    MimeBodyPart attachment = new MimeBodyPart(); 
    MimeBodyPart messagePart = new MimeBodyPart(); 
    FileDataSource fds = new FileDataSource(fileName); 

    try { 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
     message.setFrom(new InternetAddress(from)); 
     message.setSubject(subject); 
     message.setText(emailMessage); 
     attachment.setDataHandler(new DataHandler(fds)); 
     attachment.setFileName(fileName); 
     messagePart.setText(emailMessage); 
     Multipart hotmailMP = new MimeMultipart(); 
     hotmailMP.addBodyPart(attachment); 
     hotmailMP.addBodyPart(messagePart); 
     message.setContent(hotmailMP); 
     Transport transport = session.getTransport("smtp"); 
     transport.send(message); 
     JOptionPane.showMessageDialog(new JFrame() , "mail sent !");  
    } catch(Exception exc) { 
     System.out.println(exc); 
    } 
} 

Tại sao tôi nhận ngoại lệ này? Nếu có bất cứ điều gì sai với mã xin vui lòng cho biết vấn đề là gì.

+0

Tôi nghĩ không có gì sai với mã này. –

Trả lời

4

Tôi đồng ý với @ Mi Mee. Trong tên người dùng của bạn có vẻ như bạn đang dùng tên người dùng không đầy đủ (Đó là lý do Xác thực không thành công). Đối với Hotmail bạn phải nhập của bạn Windows Live Id đó có thể là [email protected] , [email protected], vv

Vì vậy, hãy ở đúng tên người dùng. Và xóa @hotmail.com khỏi biến số from. Phần còn lại của mã là tốt.

+0

vâng! tôi không để ý điều đó –

5

535 nghĩa username hoặc password xấu: xem SMTP reply codes

Bạn có thể cần phải kiểm tra thủ công máy chủ SMTP của bạn để xem những gì 5.0.0 phương tiện.

Các vấn đề liên quan