У меня проблема с отправкой писем. У меня есть хорошие SMTP и порт, но сообщение не может быть отправлено из-за этой ошибки:Отправка ошибки Gmail
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
Вот код:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
final String username = "[email protected]";
final String password = "my password";
Properties props = System.getProperties();
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", "465");
Session session;
session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress("[email protected]"));
// Set To: header field of the header.
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
//subject message
message.setSubject("Help Needed");
// Now set the actual message
message.setText("I need help please help me with this program");
message.setContent("<h:body style=background-color :white;font-family:verdana;color:#0066CC;>"
+ "If you are seeing this its OK !!!<br/><br/>"
+ "</body>", "text/html; charset=utf-8");
// Send message
Transport.send(message);
System.out.println("Sent message successfully...");
} catch (MessagingException e) {
throw new RuntimeException(e);
//System.out.println("MessagingException: "+mex.getMessage());
//JOptionPane.showMessageDialog(null, "[!]Cant connect to the database.");
}
}
Пожалуйста, ответьте, если вы знаете, я спросил daniweb, но есть не быстрые ответы ... Спасибо
Проверка этого (HTTP: // stackoverflow.com/questions/46663/how-to-send-an-email-by-java-application-using-gmail-yahoo-hotmail) –
Я уже пробовал это, но не работает, дает мне много ошибок ... – user2133393
Возможно, вы заблокировали брандмауэр. Вы находитесь в сети с брандмауэром? Если вы используете Windows, что вы видите при запуске команды «telnet smtp.gmail.com 465» (без кавычек)? Ваш брандмауэр Windows блокирует вас? – Anand