0

Windows 8 x86 と jdk_7 を使用しています。以下のコードは、エラーなしで問題なくコンパイルされます。実行すると、次の例外が発生します。

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 = "tmadile@gmail.com";

  // Sender's email ID needs to be mentioned
     String from = "hareitse@gmail.com";

  // 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();
        }
  }
}
4

1 に答える 1

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

私があなたのコードとあなたのエラーを読んだ方法は、実際にはポート 25 で localhost のメール サーバーに送信しようとしているホストとして localhost から送信しようとしているようです。SMTP サーバーがない場合localhost で実行すると、上記のエラーが発生します。あなたが提供したコードは、SMTP サーバーを起動しません。おそらく、host = "localhost" を smtp ホスト (および、smtp プロバイダーのニーズに応じて他の設定) に置き換える必要があります。

于 2013-07-29T13:34:45.990 に答える