Код: Выделить всё
Authentication failed: 535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials 5b1f17b1804b1-43656b4432dsm341121805e9.41 - gsmtp
КОД:
Код: Выделить всё
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailSender {
public static void main(String[] args) {
String to = "email";
String from = "email";
String password = "password"; // Use app-specific password if 2FA is enabled
String host = "smtp.gmail.com";
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "587"); // Using TLS
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); // Enable TLS
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Email");
message.setText("This is a test email to verify SMTP configuration.");
Transport.send(message, from, password);
System.out.println("Sent message successfully....");
} catch (AuthenticationFailedException e) {
System.err.println("Authentication failed: " + e.getMessage());
throw new RuntimeException(e);
} catch (MessagingException e) {
e.printStackTrace();
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... t-accepted