質問

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

メールサーバーを追加するときに、Adobe CF や Railo で実行できるのとまったく同じ種類のチェックが可能です。

もちろん、これはそれを保証するものではありません 配達 機能しますが、少なくとも入力されたサーバー/ユーザー名/パスが有効であることを確認する必要があります。

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

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

前もって感謝します。

編集:

Java タグの存在と混同しないでください。に必要な解決策 CFML. 。ただし、該当する場合は、いくつかの Java ライブラリを使用できます。

役に立ちましたか?

解決

Sfusseneggerには正しい考えがあると思います。しかし、カスタム認証器を使用する代わりに、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>

他のヒント

使用 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);
   }

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

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

いくつかの実用的なコードを編集します。

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]));
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top