在ColdFusion中以编程方式验证邮件服务器连接 [英] Verify mail server connection programmatically in ColdFusion

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

问题描述

我使用自订的SMTP伺服器,想在使用者输入自己的伺服器凭证时验证连线。



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



,但这并不保证 将会正常工作,而且至少可以检查输入的服务器/用户名/密码是否有效。



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



有标准的ACF / Railo发行版是否有任何Java库可以帮助我?我如何使用它们?



提前感谢。



编辑 p>

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

解决方案

我认为sfussengger有正确的想法。但是,而不是使用自定义验证器,通过connect(..)验证怎么样?只用gmail测试。但它似乎工作。



编辑:我用CF9& OBD成功。

编辑:更新为添加缺少的mail.smtp.auth属性。

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

try {
属性props =新属性();
// gmail必需
props.put(mail.smtp.starttls.enable,true);
props.put(mail.smtp.auth,true);
//或使用getDefaultInstance实例(如果需要)
会话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版本
port = 587;
host =smtp.gmail.com;
user =username@gmail.com;
pwd =email password;

try {
props = createObject(java,java.util.Properties)。
props.put(mail.smtp.starttls.enable,true);
props.put(mail.smtp.auth,true);
//或使用getDefaultInstance实例如果需要...
mailSession = createObject(java,javax.mail.Session)。getInstance(props,javacast(null,) ;
transport = mailSession.getTransport(smtp);
transport.connect(host,port,user,pwd);
transport.close();
WriteOutput(success);
}
//用于认证失败
catch(javax.mail.AuthenticationFailedException e){
WriteOutput(Error:& e.type&**& ; e.message);
}
//其他失败的
catch(javax.mail.MessagingException e){
WriteOutput(Error:& e.type&**& ; e.message);
}
< / cfscript>


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.

解决方案

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 = "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中以编程方式验证邮件服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆