Написал код для отправки письма на почту, но он выдает ошибку:
Could not connect to SMTP host: smtp.gmail.com, port: 465;
String host = "smtp.gmail.com";
String username = "myEmail@gmail.com";
String password = "myPassword";
String recipientEmail = "recipientEmail@gmail.com";
String subject = "Тема письма";
String message = "Текст письма.";
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.ssl.trust", "true");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.debug", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(username));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
Transport.send(mimeMessage);
System.out.println("Письмо успешно отправлено.");
} catch (MessagingException ex) {
ex.printStackTrace();
System.out.println("Ошибка при отправке письма.");
}