添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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

Exception: javax.mail.AuthenticationFailedException: failed to connect, no password specified? Even with PasswordAuthenticator?

Ask Question

I am making a java application that sends emails.

For some reason, I kept getting javax.mail.AuthenticationFailedException , even though I have the correct username and password.

I've seen some posts say that the missing password authentication might cause this exception as well, but I think i have that part as well. Now I am so stuck and couldn't figure out where things go wrong.

here is my code:

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import javax.swing.*;
public class sendEmail {
    private static String subject = "Test", content = "This is a test";
    public sendEmail() {
    public static void send() throws MessagingException{
        Properties properties = new Properties();
        properties.put("mail.smtp.auth","true");// set the authentication to true
        properties.put("mail.smtp.starttls.enable","true");
        properties.put("mail.smtp.host","smtp.gmail.com");
        properties.put("mail.smtp.port","587");
        String username = "clashroyale70532@gmail.com";
        String password = "********";
        Session ses = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPassWordAuthentication() {
                return new PasswordAuthentication(username, password);
        ses.setDebug(true);
        Message message = prepareMessage(ses,username);
        Transport.send(message);
    private static Message prepareMessage(Session session, String account) {
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(account));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(account));
            msg.setSubject(subject);
            msg.setText(content);
            return msg;
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException m) {
            throw new RuntimeException(m);
        return null;
    public static void main(String[] args) {
        try {
            sendEmail.send();
            catch (MessagingException m) {
                m.printStackTrace();

Here is the JavaMail debug output:

 DEBUG: setDebug: JavaMail version 1.6.2
    DEBUG: getProvider() returning 
    javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
    DEBUG SMTP: need username and password for authentication
    DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=*******(name not shown, it's my real name), password=<null>

The user it shows is not associated with the email account I used in my code. I am not sure how it got my real name.

it seems your issue is with gmail server setting check this link stackoverflow.com/questions/43406528/… – divyang4481 Jan 13, 2020 at 4:11 Does this answer your question? JavaMail with Gmail: 535-5.7.1 Username and Password not accepted – divyang4481 Jan 13, 2020 at 4:11 I turn on less secured app access already. I think this is a different problem because: 1. diffrent error name; 2. I have successfully sent some emails using ssl instead of smtp with the same account – Cloud Walker Jan 13, 2020 at 4:15 I am new to java. Learning and understanding are my main focus. I really want to know what is causing the problem instead of just skip it without fully understand it. I included the JavaMail debug output. At the end of the debug output, it supposed to be "...password=<null>" Not sure why it's not showing in the question part. – Cloud Walker Jan 14, 2020 at 2:50

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.