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

与Adobe CF和Railo添加邮件服务器时允许使用完全相同的检查。

当然,这不能保证 交货 将工作,但至少要检查输入的服务器/用户名/通行证是否有效。

我可以看到一种棘手的方法:尝试发送电子邮件 cfmail 并检查邮件日志。但是我相信可以更优雅地完成。

标准ACF/Railo发行版提供了任何Java库来帮助我吗?我将如何使用它们?示例非常感谢。

提前致谢。

编辑:

请不要与存在的Java标签混淆。需要解决方案 CFML. 。尽管可以使用一些Java库,如果适用。

有帮助吗?

解决方案

我认为Sfussenegger有正确的主意。但是,不用使用自定义身份验证器,而是通过Connect(..)进行身份验证如何?仅通过Gmail测试。但这似乎有效。

编辑: 我成功地用CF9和OBD对此进行了测试。不幸的是,我对Railo没有运气... bummer。

编辑: 更新以添加缺少的“ 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并没有抱怨非法@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