Javax.mail.AuthenticationFailedException: 535 5.0.0 Authentication Error

I do not understand why I am getting this exception. This is the code that is trying to send an email.

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); } } 

Why am I getting this exception? If something is wrong with the code, please let me know what the problem is.

+6
source share
2 answers

I agree with @Mi Mee. It appears in your username that you are accepting an incomplete username (this is the reason for the authentication error ). For Hotmail, you need to enter the Windows Live ID , which can be xyz@hotmail.com , qrs@gmail.com , etc.

So enter the correct username. And remove @hotmail.com from the from variable. The rest of the code is fine.

+4
source

535 means invalid username or password: see SMTP response codes

You may need to check the SMTP server manual to find out what 5.0.0 means.

+5
source

Source: https://habr.com/ru/post/893411/


All Articles