hello guys im trying to send email from java program but when i pressing the button i get this message:
errorjavax.mail.AuthenticationFailedException: failed to connect, no password specified?
and here is the code:
String username = Username.getText();
String password = Password.getText();
final String username2 = "stefanrafaa@gmail.com";
final String password2 = "my pass";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session;
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication(URLName name) {
return new javax.mail.PasswordAuthentication(username2, password2);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("stefanrafa0@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("stefanrafaa@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
System.out.println("error" + e);
}
try {
File folder = new File(username);
File file = new File(folder, username + ".txt");
FileReader freader = new FileReader(file);
BufferedReader breader = new BufferedReader(freader);
String line = breader.readLine();
String[] parts = line.split("=");
String key = parts[0].trim();
String value = parts[1].trim();
if (key.equals("Password") && value.equals(password)) {
showError2(true);
new Thread(new Start(this)).start();
LabelInfo.setForeground(Color.green);
LabelInfo.setText("Password Accepted");
} else {
LabelInfo.setText("Wrong password");
showError(true);
}
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
LabelInfo.setForeground(Color.red);
LabelInfo.setText("User doesn't exist");
showError(false);
showError2(false);
}