1

Java で電子メール アプリケーションを作成しているときに、次の例外が発生します。

javax.mail.MessagingException: Could not connect to SMTP host: mail.simsystech.com, port: 8443;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:317)

&ここに私のコードがあります(このチュートリアルリンクに従ってください):

          Properties properties = System.getProperties();
          properties.put("mail.smtp.starttls.enable", "true");
          properties.put("mail.transport.protocol", "smtp");
          properties.setProperty("mail.smtp.host", host);
//        properties.put("mail.smtp.port", "8443");
          properties.setProperty("mail.smtp.auth", "true");
          properties.setProperty("mail.smtp.starttls.enable", "true");

          final String user = "abc@xyz.com";
          final String password = "*****";

          Authenticator authenticator = new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user,password);
                }
            };


          Session session = Session.getDefaultInstance(properties,authenticator);


          try{
              MimeMessage message = new MimeMessage(session);

              message.setFrom(new InternetAddress(sid));

              message.addRecipient(Message.RecipientType.TO,
                                       new InternetAddress(rid));

              message.setSubject(subject);

              BodyPart messageBodyPart = new MimeBodyPart();

              messageBodyPart.setText(text);

              Multipart multipart = new MimeMultipart();

              multipart.addBodyPart(messageBodyPart);


              messageBodyPart = new MimeBodyPart();
//            String filename = "file.txt";
              DataSource source = new FileDataSource(file);
              messageBodyPart.setDataHandler(new DataHandler(source));
              messageBodyPart.setFileName(file);
              multipart.addBodyPart(messageBodyPart);

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


              // Now set the actual message
//            message.setText(text);

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

私はstackoverflowのこれらのリンクを通過しましたが:1、2

なぜこのエラーが発生するのか... :( :(

4

2 に答える 2

3

なぜこのエラーが発生するのか...

最も明白な潜在的な原因は次の 2 つです。

  • SMTP ホストのホスト名やポート番号が間違っています。そのポートを使用してそのホストで実行されている SMTP サービスがない場合は、別のものを使用する必要があります。

  • ファイアウォールがアクセスをブロックしているため、コンピュータはそのポートでそのホストに接続できません。

于 2012-10-04T13:28:30.127 に答える
1

接続にネットワークの問題が発生しないと仮定すると(telnetを使用して確認できます)、...

問題の原因はproperties.setProperty("mail.smtp.starttls.enable", "true");

com.sun.mail.smtpの javadoc によると 、

true の場合、STARTTLS コマンド (サーバーでサポートされている場合) を使用して、ログイン コマンドを発行する前に接続を TLS で保護された接続に切り替えることができます。クライアントがサーバーの証明書を信頼するように、適切な信頼ストアを構成する必要があることに注意してください。デフォルトは false です。

そして以下はmail.smtp.starttls.required

true の場合、STARTTLS コマンドを使用する必要があります。サーバーが STARTTLS コマンドをサポートしていない場合、またはコマンドが失敗した場合、connect メソッドは失敗します。デフォルトは false です。

プロバイダーがサポートしていない場合は、上記のプロパティを削除してみてください。

于 2012-10-04T13:39:24.100 に答える