This program attempts to send e-mail but throws a run time exception:
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
Why am I getting this exception when I have supplied the correct username and password for authentication?
Both the sender and receiver have g-mail accounts. The sender and the receiver both have g-mail accounts. The sender has 2-step verification process disabled.
This is the code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class tester {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.smtp.host" , "smtp.gmail.com");
props.put("mail.stmp.user" , "username");
//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
//To use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance( props , null);
String to = "me@gmail.com";
String from = "from@gmail.com";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com" , 465 , "username", "password");
transport.send(msg);
System.out.println("fine!!");
}
catch(Exception exc) {
System.out.println(exc);
}
}
}
Even after giving the password I get the exception. Why is it not authenticating?
Turn On "Access for less secure apps" in Security setting for the gmail account.(from mail), see the below link for references
http://www.ghacks.net/2014/07/21/gmail-starts-block-less-secure-apps-enable-access/
This error may be about password characters. If your password contains special characters and also you add your password into
Transport
class methods;For Example
or
you may get that error. Because
Transport
class' methods may not handle special characters. If you add your username and password into your message usingjavax.mail.PasswordAuthentication
class, i hope you will escape that error;For Example
I've solved this issue adding user and password in
Transport.send
call:According to this signature of the
send
function in javax.mail (from version 1.5):public static void send(Message msg, String user, String password)
Also, if you use this signature it's not necessary to set up any
Authenticator
, and to set user and password in theProperties
(only the host is needed). So your code could be:I also have this problem so don't worry. It comes from mail server side due to an outside authentication issue. Open your mail and you will get a mail from the mail server telling you to enable accessibility. When you have done that, retry your program.
Your email session should be provided an authenticator instance as below
a complete example is here http://bharatonjava.wordpress.com/2012/08/27/sending-email-using-java-mail-api/