20

アプリケーションでJavaMailAPIを使用して、以前よりも自動化された方法でいくつかのファイルを送信するのに問題があります。私はJavaとNetBeansを初めて使用しますが、他の言語でプログラミングしたことがあるので、JavaやNetBeansに少し迷ったように思われる場合はご容赦ください。

このエラーが発生し続ける

java.net.SocketException:アクセスが拒否されました:接続

ローカルメールサーバーに接続しようとしたとき。ユーザー名、パスワード、ポートを変更するだけで、同じコードでGmailのSMTPサーバーに接続してメールを正常に送信しました。また、サーバーにTelnetで接続し、ポート25から220応答を取得することもできました。バッチファイルも実行され、ローカルサーバーを介して電子メールを正常に送信します。なぜ私が接続できないのかについての考えやアイデアはありJavaMailますか?

これが電子メールを送信するコードです。

ソースコード:

public void sendEmail(String customerNumber, ArrayList fileList){
   String from = "xxxx";
   String username = "xxxx";
   String to = "xxxx";
   String host = "10.1.1.6";
   String pwd = "xxxx";
   String port = "25";

   Properties props = System.getProperties();
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.port", port);
   props.put("mail.smtp.user", username);
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtp.debug", "true");
   props.put("mail.smtp.socketFactory.port", port);
   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
   props.put("mail.smtp.socketFactory.fallback", "false");

   Session session = Session.getInstance(props, null);
   session.setDebug(true);

   MimeMessage message = new MimeMessage(session);
   try{
       message.setFrom(new InternetAddress(from));
       message.setRecipients(Message.RecipientType.TO, to);
       message.setSubject("Electronic Invoices");
       BodyPart messageBodyPart = new MimeBodyPart();
       messageBodyPart.setText("Electronic Invoices");
       Multipart multipart = new MimeMultipart();
       multipart.addBodyPart(messageBodyPart);
       for(int i = 0; i < fileList.size(); i++){
           messageBodyPart = new MimeBodyPart();
           String fileName = (String) fileList.get(i);
           DataSource source = new FileDataSource(fileName);
           messageBodyPart.setDataHandler(new DataHandler(source));
           messageBodyPart.setFileName(fileName);
           multipart.addBodyPart(messageBodyPart);
       }
       message.setContent(multipart);

       Transport tr;
       tr = session.getTransport("smtp");
       tr.connect(host, username, pwd);
       tr.sendMessage(message, message.getAllRecipients());
       jTextArea2.append("Mail Sent Successfully");
       tr.close();

   } catch(Exception e){
       jTextArea2.append("sendEmail()::" + System.getProperty("line.separator") + e + System.getProperty("line.separator"));
       System.out.println(e.getMessage());
       System.out.println(e.getCause());
   }
}

2つのExceptionステートメントからの出力:

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "10.1.1.6", port 25, isSSL false
Could not connect to SMTP host: 10.1.1.6, port: 25
java.net.SocketException: Permission denied: connect
4

2 に答える 2

20

-Djava.net.preferIPv4Stack=trueVMオプションに追加します。同じ問題であるかどうかを確認する別の方法として、Netbeansで、プロジェクト>プロパティ>ライブラリを右クリックし、JDK 6 Javaプラットフォームを選択します(お持ちでない場合はインストールしてください)。クリーンアップしてビルドし、再試行してください。これにより、この問題が問題として解消されます

クレジットhttps://stackoverflow.com/a/7478027/643500

于 2012-10-15T18:56:35.893 に答える
2

開始時にコードで使用されるアプリの呼び出し(CLIなどから)を簡素化する場合:

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

アプリがレガシーIPv4ネットワークスタックで動作すると仮定します。

于 2017-03-16T15:03:52.550 に答える