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

时间:2023-01-15
本文介绍了在 ColdFusion 中以编程方式验证邮件服务器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

当然,这并不能保证 delivery 会正常工作,但至少要检查输入的服务器/用户名/密码是否有效.

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

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

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.

标准 ACF/Railo 发行版中是否有任何 Java 库可以帮助我?我将如何使用它们?非常感谢示例.

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

提前致谢.

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

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

推荐答案

我认为 sfussenegger 的想法是对的.但是不是使用自定义身份验证器,而是通过 connect(..) 进行身份验证呢?只用gmail测试过.但它似乎工作.

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.

我用 CF9 &OBD成功.不幸的是,我在 Railo 上没有运气……真可惜.

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

已更新以添加缺少的mail.smtp.auth"属性.它现在应该也可以与 Railo 一起正常工作了.

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 = "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>

这篇关于在 ColdFusion 中以编程方式验证邮件服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Railo 中将 Java 结果集转换为 ColdFusion 查询 下一篇:FTP - 以编程方式确定剩余时间或传输字节的方法

相关文章

最新文章