在發送電子郵件我得到這個錯誤無法連接到SMTP主機:smtp.gmail.com端口:465,響應:-1
java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
我的代碼是:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("email"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(this.to));
message.setSubject("Testing");
message.setText("Hey, this is the testing email.");
Transport.send(message);
任何幫助,將不勝感激。
在此先感謝。
檢查http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ – Abi 2013-03-13 06:14:53
我試過相同的代碼,但仍然得到錯誤:javax.mail.MessagingException :530 5.7.0必須首先發出STARTTLS命令。 4sm28216799pbn.23 - gsmtp \t在com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)在com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716) \t在COM。 sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388) \t at rapid.mail.main(mail.java:60)starttls的這一行是那裏,那麼爲什麼得到相同的錯誤:properties.put(「mail .smtp.starttls.enable「,」true「); – user1900376 2013-03-13 06:23:11
嘗試使用最新的jar版本JavaMail 1.4.7 – Abi 2013-03-13 06:36:11