我有Windows 7系統沒有安裝smtp服務器,只有java。我想通過gmail從我的程序發送郵件。我已經寫了一個Java程序來通過smtp連接谷歌,並啓用了telnet選項,但我得到下面的錯誤。我嘗試使用端口465和587,但沒有改變。使用java從您的計算機發送郵件需要什麼?
錯誤:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at mail1.SendMailSSL.main(SendMailSSL.java:44)
代碼:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","mypassword");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"[email protected]"));
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) {
throw new RuntimeException(e);
}
}
}
誰能告訴我什麼,我需要做的,從我個人的系統中發送電子郵件?請一步一步告訴我,幷包括安裝軟件等任何內容,因爲我必須爲我的項目執行此操作。你的回答非常感謝。先謝謝你。
檢查您的電腦中的系統日期是否正確 – 2012-03-10 10:26:59
骯髒的(但工作中)的解決方案是更改您的SocketFactory的執行巫婆不檢查證書鏈的有效性。例如,您有EasySSLProtocolSocketFactory實現(來自Apache commons-httpclient-contrib包)。 – 2012-03-10 10:31:05
非常感謝guido我犯了錯誤,我的電腦有舊日期.. – BOBBY 2012-03-11 02:04:18