Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Attempting to implement sending an email through SMTP in Java, but get the error "AUTH LOGIN failed"

Ask Question

I have been banging my head against my desk for the past couple of days trying to figure out why I keep getting the following errors when attempting to implement Java code for sending an email through my company's Outlook server:

DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "main" javax.mail.AuthenticationFailedException: 535 5.7.0 authentication failed
    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)

Here is the code in question:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail3 {
    public static void main(String[] args) throws Exception {
        Login e = new Login();
        String host = "mail.authsmtp.com";
        String port = "465";
        final String userName = e.getEmailUser();
        final String password = e.getEmailPassword();
        String toAddress = "first.last@email";
        String subject = "TEST";
        String message = "This is a test email";
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.debug", "true");
        // properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.user", userName);
        properties.put("mail.password", password);
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
        Session session = Session.getInstance(properties, auth);
        // creates a new e-mail message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(message, "text/html");
        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        // sets the multi-part as e-mail's content
        msg.setContent(multipart);
        // sends the e-mail
        Transport.send(msg);

The error is triggered at Transport.send(msg). Any guidance will be greatly appreciated.

did you print out e.getEmailUser() and e.getEmailPassword() and verify that they're correct – Solace Nov 16, 2016 at 18:08 I know for a fact that e.getEmailPassword() is correct (unless my Outlook password isn't the same as my Windows 7 password, which means I was wrong all along). e.getEmailUser() is "first.last@company.com", but I am starting to think that might have to be written differently. – M P Nov 16, 2016 at 22:13

535 code stands for bad username or password.

Make sure your email user and password are correct by debugging this code.

Another tip is to login to mail via web or maybe try to change password to mail.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.