6

カスタム SMTP サーバーを使用しており、ユーザーが自分のサーバー資格情報を入力したときに接続を確認したいと考えています。

Adobe CF および Railo がメール サーバーを追加するときに実行できるのとまったく同じタイプのチェック。

確かに、これは配信が機能することを保証するものではありませんが、少なくとも入力されたサーバー/ユーザー名/パスが有効であることを確認するために.

巧妙な方法が 1 つあります。cfmailを使用してメールを送信し、メール ログを確認してみてください。しかし、それはもっとエレガントにできると信じています。

標準の ACF/Railo ディストリビューションで利用できる Java ライブラリはありますか? それらをどのように使用しますか?例は高く評価されています。

前もって感謝します。

編集:

Java タグの存在と混同しないでください。CFMLで必要なソリューション。ただし、該当する場合は、いくつかの Java ライブラリを使用できます。

4

3 に答える 3

7

スフッセネッガーの考えは正しいと思います。しかし、カスタム オーセンティケータを使用する代わりに、connect(..) を介して認証するのはどうでしょうか? gmail でのみテストされています。しかし、うまくいくようです。

編集: CF9 & OBD でこれを正常にテストしました。残念ながら、Railo には運がありませんでした。残念です。

編集:不足している「mail.smtp.auth」プロパティを追加するために更新されました。Railo でも正しく動作するはずです。

    //Java Version
    int port = 587;
    String host = "smtp.gmail.com";
    String user = "username@gmail.com";
    String pwd = "email password";

    try {
        Properties props = new Properties();
        // required for gmail 
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        // or use getDefaultInstance instance if desired...
        Session session = Session.getInstance(props, null);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, port, user, pwd);
        transport.close();
        System.out.println("success");
     } 
     catch(AuthenticationFailedException e) {
           System.out.println("AuthenticationFailedException - for authentication failures");
           e.printStackTrace();
     }
     catch(MessagingException e) {
           System.out.println("for other failures");
           e.printStackTrace();
     }



<cfscript>
    //CF Version
    port = 587;
    host = "smtp.gmail.com";
    user = "username@gmail.com";
    pwd = "email password";

    try {
        props = createObject("java", "java.util.Properties").init();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        // or use getDefaultInstance instance if desired...
        mailSession = createObject("java", "javax.mail.Session").getInstance(props, javacast("null", ""));
        transport = mailSession.getTransport("smtp");
        transport.connect(host, port, user, pwd);
        transport.close();
        WriteOutput("success");
     } 
     //for authentication failures
     catch(javax.mail.AuthenticationFailedException e) {
           WriteOutput("Error: "& e.type &" ** "& e.message);
     }
     // for other failures
     catch(javax.mail.MessagingException e) {
           WriteOutput("Error: "& e.type &" ** "& e.message);
     }
</cfscript>
于 2010-04-23T01:42:15.117 に答える
1

Apache Commons Netを使用すると、次のようなことができます。

try {
     int reply;
     client.connect("mail.foobar.com");
     System.out.print(client.getReplyString());
     // After connection attempt, you should check the reply code to verify
     // success.
     reply = client.getReplyCode();
     if(!SMTPReply.isPositiveCompletion(reply)) {
       client.disconnect();
       System.err.println("SMTP server refused connection.");
       System.exit(1);
     }
     // Do useful stuff here.
     ...
   } catch(IOException e) {
     if(client.isConnected()) {
       try {
         client.disconnect();
       } catch(IOException f) {
         // do nothing
       }
     }
     System.err.println("Could not connect to server.");
     e.printStackTrace();
     System.exit(1);
   }

org.apache.commons.net.smtp.SMTPClientクラスclientのインスタンスです。上記のコードは、SMTPClient API ドキュメントから取得したものです。

于 2010-04-22T08:32:02.870 に答える
0

それはきれいではありませんが、可能です。不正なアドレスに電子メールを送信してみて、どのようなエラーメッセージが表示されるかを確認してください。エラーメッセージが認証の失敗について文句を言う場合、あなたは何をすべきかを知っています。

いくつかの作業コードを編集します:

Gmailのクレデンシャルを検証するための実用的なコードを次に示します。ただし、Gmailはillegal@localhostについて文句を言いません。これで、例外の原因となる受信者を検索するか、実際にメールをアドレスに送信してすぐに破棄することができます。。編集:何かを送る必要すらありません。可能性のあるAuthenticationFailedExceptionに接続して処理するだけです。

import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;

public class Main {

    private static Session createSmtpSession(final String user, final String password) {
        final Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.starttls.enable", "true");

        return Session.getDefaultInstance(props, new javax.mail.Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        });
    }

    private static boolean validateCredentials(String user, String password) {
        try {
            Transport transport = createSmtpSession(user, password).getTransport();
            transport.connect();
            transport.close();
        } catch (AuthenticationFailedException e) {
            return false;
        } catch (MessagingException e) {
            throw new RuntimeException("validate failed", e);
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(validateCredentials(args[0], args[1]));
    }

}
于 2010-04-22T08:23:21.297 に答える