2013-07-29 51 views
0

我正在使用Windows 8 x86和jdk_7。下面的代碼compliles很好沒有錯誤。當我運行它給了我這樣的例外:我如何解決在Windows 8中的Java 7 FTP傳輸8

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; 
    nested exception is: 
java.net.SocketException: Permission denied: connect 

現在,作爲一種變通方法,我想加入這一行:

System.setProperty("java.net.preferIPv4Stack", "true"); 

這並沒有解決問題,並在網上查詢了幾個小時之後,我注意到問題在於jdk_7和防火牆問題。我甚至試過這個命令

netsh advfirewall set global StatefulFTP disable 

這也不能解決問題。

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

public class SendFileEmail 
{ 
    public static void main(String [] args) 
    { 
    System.setProperty("java.net.preferIPv4Stack", "true"); 
    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 

    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 

    // Assuming you are sending email from localhost 
    String host = "localhost"; 

    // Get system properties 
    Properties properties = System.getProperties(); 

    // Setup mail server 
    properties.setProperty("mail.smtp.host", host); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
    // Create a default MimeMessage object. 
     MimeMessage message = new MimeMessage(session); 

    // Set From: header field of the header. 
     message.setFrom(new InternetAddress(from)); 

    // Set To: header field of the header. 
     message.addRecipient(Message.RecipientType.TO, 
           new InternetAddress(to)); 

    // Set Subject: header field 
     message.setSubject("This is the Subject Line!"); 

    // Create the message part 
     BodyPart messageBodyPart = new MimeBodyPart(); 

    // Fill the message 
     messageBodyPart.setText("This is message body"); 

    // Create a multipar message 
     Multipart multipart = new MimeMultipart(); 

    // Set text message part 
     multipart.addBodyPart(messageBodyPart); 

    // Part two is attachment 
     messageBodyPart = new MimeBodyPart(); 
     String filename = "shh.jpg"; 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(filename); 
     multipart.addBodyPart(messageBodyPart); 

    // Send the complete message parts 
     message.setContent(multipart); 

    // Send message 
     Transport.send(message); 
     System.out.println("Sent message successfully...."); 
    } 
     catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } 
    } 
} 
+0

只要您不在本地主機上運行SMTP服務器,它將不起作用。 – mthmulders

回答

1
// Assuming you are sending email from localhost 
String host = "localhost"; 
// Setup mail server 
properties.setProperty("mail.smtp.host", host); 

我看了你的代碼,你的錯誤似乎更喜歡你正試圖從本地主機發送一個主機,你事實上是在試圖在本地主機發送到郵件服務器的端口的方式25.如果你沒有在本地主機上運行任何SMTP服務器,你將會得到上述錯誤。您提供的代碼不會爲您啓動任何SMTP服務器。您應該用您的smtp主機替換host =「localhost」(並根據您的smtp提供程序的需要進行其他設置)。

+0

謝謝你。我這樣做,但現在我得到了異常:'com.sun.mail.smtp.SMTPSendFailedException:451 SMTP中繼連接超時' –

+0

雖然這是更糟。至少它表示你的連接通過你的smtp服務器。難道你的提供者不允許你通過他們的服務器傳遞消息。這對我來說是這樣,但是在聯繫我的ISP並獲得郵件服務器的登錄後,他們將允許我發送其他發件人地址的電子郵件,而不是他們所期望的。另請參閱以下幫助(搜索451)。 [鏈接](http://www.answersthatwork.com/Download_Area/ATW_Library/Networking/Network__3-SMTP_Server_Status_Codes_and_SMTP_Error_Codes.pdf) – DanielBarbarian