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
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.
–
–
–
–
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.