我正嘗試使用java將電子郵件發送到Gmail。我正在使用此代碼。使用java超時異常發送電子郵件
final String username = "[email protected]";
final String password = "**********";
Properties props = new Properties();
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","587");
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO,newInternetAddress("[email protected]"));
message.setSubject("First email to using java");
message.setContent("<h:body style =background-color:white> This is a test mail sent using java" + "</body>","text/html; charset=utf-8");
Transport.send(message);
System.out.println("Message Sent");
但是當我運行上面的代碼它顯示follwoing錯誤:
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
我有一個使用代理服務器,並需要身份驗證Internet連接。是因爲代理服務器出現此錯誤還是代碼中存在一些問題。請告訴我如何解決它。
提供的鏈接說**的JavaMail不支持通過HTTP代理服務器的Web服務器訪問**這意味着它無法發送電子郵件。在使用java的http代理服務器上還是有一些辦法? –
如果您擁有的只是一個http代理服務器,則無法使用JavaMail直接通過它進行連接。這就是說,這不是**不可能**。 HTTP確實支持可以使用的「隧道」功能,但JavaMail使用的任何底層java.net類都不支持它,而JavaMail沒有直接支持它。您可以使用其他答案中提到的[Corkscrew](http://en.wikipedia.org/wiki/Corkscrew_%28program%29)等程序間接進行操作。 –