Verify mail server connection programmatically in ColdFusion

Sergey Galashyn picture Sergey Galashyn · Apr 22, 2010 · Viewed 12.3k times · Source

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.

Answer

Leigh picture Leigh · Apr 23, 2010

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>