2016-12-08 8 views
1

解決策:Javaコードは問題ではありませんでした。 Postfixサーバは、非ローカルIP /ホストから発信された電子メールを受け入れるように設定されていませんでした。 IPは、mynetworks変数のmain.cfに追加されました。元の投稿を文脈のままにしておきます。無効なアドレスを返すPostfix。 454 <to @ domain.com>:リレーアクセスが拒否されました - 解決済み

私はあらかじめ設定されたPostfixサーバを使用しており、JavaMailを使ってそれと対話しています。私はそれを要求する可能性のある外部ドメインに電子メール通知を送信しようとしています。ここで

は、テストメールを送信するJavaメソッドです:私は、JavaMailのコードが含まれているメソッドを実行すると

public void TestEmail() { 

    String to = "[email protected]"; 

    String from = "[email protected]"; 
    final String username = "user"; 
    final String password = "password"; 

    String host = "xxx.xxx.xxx.xxx"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
      new Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
    }); 
    try { 
     Message message = new MimeMessage(session); 

     message.setFrom(new InternetAddress(from)); 

     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 

     message.setSubject("Test Email"); 

     message.setContent("This is a test email", "text/html"); 

     Transport.send(message); 

     System.out.println("The test message was sent!"); 
    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 

} 

、私は
generalError: javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 454 4.7.1 <[email protected]>: Relay access denied

を取得し、私はから送信メールをテストすることができましたPostfixサーバは
echo "This is the body" | mail -s "This is the subject" [email protected]
を使用してメールを受信しました。

私はPostfixサーバ上のすべての設定を確認しているわけではありませんが、それはどこに問題があるのだろうと思っていますが、十分に慣れていないので、 - ニンリー。だから、どんな助けでも大歓迎です。

ありがとうございました!

EDIT:

String host = "xxx.xxx.xxx.xxx"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", "587"); 

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

    try { 
     Message message = new MimeMessage(session); 

     message.setFrom(new InternetAddress(from)); 

     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 

     message.setSubject("Test Email"); 

     message.setContent("This is a test email", "text/html"); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, 587, username, password); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 

     System.out.println("The test message was sent!"); 
    } catch (MessagingException e) { 
     System.out.println("Error: " + e); 
     e.printStackTrace(); 
     throw new RuntimeException(e); 
    } 

} 

これは持っている: 私は、コードの全体の新しいセットがあるセッションの作成から設定Authenticator()を削除し、推奨コードブロック

 Transport transport = session.getTransport("smtp"); 
     transport.connect(host, 587, username, password); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 

Transport.send()を置き換えます受信しているエラーメッセージを変更しました:Unrecognized SSL message, plaintext connection?

答えて

1

This article詳細情報があります。

まず、JavaMail FAQに記載されているようにオーセンティケータを削除します。そうすれば、実際にサーバーに認証しようとしていることが保証されます。 JavaMail session debuggingをオンにして、認証されていることを確認します。あなたは

Transport.send(message, username, password); 

Transport transport = session.getTransport("smtp"); 
    transport.connect(host, 587, username, password); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 

を置き換えることができます

注あなたはmail.smtp.auth設定を取り除くが、他の特性を維持することができます。

most current version of JavaMailを使用していることを確認してください。

これでもうまくいかない場合は、上記の記事でPostfixの設定を変更する手がかりが得られるかもしれません。

+0

ありがとうございます!私はJavaMail FAQから推奨されているように変更しました。私はJavaMailの最新バージョン1.5.6を使用しています。私は今少なくとも別のエラーメッセージを受け取ります。認識されないSSLメッセージ、プレーンテキスト接続ですか?だから私はそこから何が分かるかを見ていきます。再度、感謝します! – the3rdNotch

+0

現在使用しているコードであなたの投稿を更新してください。エラーメッセージは、SSLではなくプレーンテキストを使用しているサーバーポートにSSLで接続していることを示しています。上のコードは正しいように見えます(開始点を使用しています)ので、別のものを変更しておく必要があります。 –

+0

元の投稿に更新されたコードを追加しました。 – the3rdNotch

関連する問題