在 ColdFusion 中以编程方式验证邮件服务器连接

发布于 2024-08-29 15:18:11 字数 406 浏览 9 评论 0原文

我正在使用自定义 SMTP 服务器,并且希望在用户输入自己的服务器凭据时验证连接。

与 Adob​​e CF 和 Railo 在添加邮件服务器时允许执行的检查类型完全相同。

当然,这并不能保证交付正常进行,但至少可以检查输入的服务器/用户名/密码是否有效。

我可以看到一种棘手的方法:尝试使用cfmail发送电子邮件并检查邮件日志。但我相信它可以做得更优雅。

标准 ACF/Railo 发行版是否有任何 Java 库可以帮助我?我将如何使用它们?示例受到高度赞赏。

提前致谢。

编辑:

请不要与存在的 Java 标签相混淆。 CFML 中需要解决方案。虽然它可以使用一些 Java 库(如果适用)。

I'm using custom SMTP servers and would like to verify the connection when user enters his own server credentials.

Exactly the same type of check as Adobe CF and Railo allow to do when adding mail server.

Sure, this does not guarantee that delivery will be working, but at least to check that entered server/username/pass are valid.

I can see one tricky way: try to send the email with cfmail and check the mail log. But I believe that it can be done with more elegant.

Is there any Java library available with standard ACF/Railo distro to help me? How would I use them? Examples are highly appreciated.

Thanks in advance.

EDIT:

Please don't be confused with Java tag present. Solution needed in CFML. Though it can use some Java libraries, if applicable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沐歌 2024-09-05 15:18:29

这不太漂亮,但可以做到:只需使用 try 向非法地址发送电子邮件,然后查看收到的错误消息。如果错误消息抱怨身份验证失败,您知道该怎么做。

编辑一些工作代码:

这里有一些用于验证 Gmail 凭据的工作代码。 Gmail 不会抱怨非法@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]));
    }

}

That's not pretty, but could do: simply use try to send an email to an illegal address and see what error message you get. If the error message complains about failed authentication, you know what to do.

EDIT some working code:

here's some working code to validate Gmail credentials. Gmail doesn't complain about the illegal@localhost though. Now you could either try to search for a recipient that causes an exception or really send mail to an address and discard it immediately.. Edit: It's not even necessary to send something. Simply connect and handle a possible 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]));
    }

}
玻璃人 2024-09-05 15:18:27

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

Where clientorg.apache.commons.net.smtp.SMTPClient 类。上面的代码取自 SMTPClient API 文档。

Using Apache Commons Net, you can do something like this:

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

Where client is an instance of org.apache.commons.net.smtp.SMTPClient class. Code above was taken from the SMTPClient API Docs.

暖心男生 2024-09-05 15:18:25

我认为 sfussenegger 的想法是正确的。但是,不使用自定义身份验证器,通过 connect(..) 进行身份验证怎么样?只用 Gmail 测试过。但这似乎有效。

编辑:我用CF9 & 测试了这一点。 OBD成功。不幸的是,我对 Railo 没有什么好感……真是太糟糕了。

编辑:更新以添加缺少的“mail.smtp.auth”属性。现在它也应该可以与 Railo 一起正常工作。

    //Java Version
    int port = 587;
    String host = "smtp.gmail.com";
    String user = "[email protected]";
    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 = "[email protected]";
    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>

I think sfussenegger has the right idea. But instead of using a custom authenticator, what about authenticating via connect(..)? Only tested with gmail. But it seems to work.

EDIT: I tested this with CF9 & OBD successfully. Unfortunately, I had no luck with Railo ... bummer.

EDIT: Updated to add the missing "mail.smtp.auth" property. It should now work correctly with Railo as well.

    //Java Version
    int port = 587;
    String host = "smtp.gmail.com";
    String user = "[email protected]";
    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 = "[email protected]";
    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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文