001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.mail;
020
021import java.util.Properties;
022
023import javax.mail.Authenticator;
024import javax.mail.MessagingException;
025import javax.mail.Session;
026import javax.mail.Transport;
027
028import lucee.commons.lang.StringUtil;
029
030import org.apache.commons.mail.DefaultAuthenticator;
031
032/**
033 * SMTP Server verifier
034 */
035public final class SMTPVerifier{       
036       
037    /**
038     * verify mail server
039     * @param host
040     * @param username
041     * @param password
042     * @param port
043     * @return are the setting ok
044     * @throws SMTPException 
045     */
046    public static boolean verify(String host, String username,String password, int port) throws SMTPException  {
047        try {
048            return _verify(host,username,password,port);
049        } 
050        catch (MessagingException e) {
051            
052            // check user
053            if(!StringUtil.isEmpty(username)) {
054                try {
055                    _verify(host,null,null,port);
056                    throw new SMTPExceptionImpl("can't connect to mail server, authentication settings are invalid");
057                } catch (MessagingException e1) {
058                    
059                }
060            }
061            // check port
062            if(port>0 && port!=25) {
063                try {
064                    _verify(host,null,null,25);
065                    throw new SMTPExceptionImpl("can't connect to mail server, port definition is invalid");
066                } 
067                catch (MessagingException e1) {}
068            }
069            
070            throw new SMTPExceptionImpl("can't connect to mail server");
071        }
072    }
073        
074    private static boolean _verify(String host, String username,String password, int port) throws MessagingException {
075        boolean hasAuth=!StringUtil.isEmpty(username);
076        
077        Properties props=new Properties();
078        props.put("mail.smtp.host", host );  
079        if(hasAuth)props.put("mail.smtp.auth", "true" );  
080        if(hasAuth)props.put("mail.smtp.user", username ); 
081        if(hasAuth)props.put("mail.transport.connect-timeout", "30" );  
082        if(port>0)props.put("mail.smtp.port", String.valueOf(port) );
083        
084        
085        Authenticator auth=null;
086        if(hasAuth)auth=new DefaultAuthenticator(username,password);
087        Session session = Session.getInstance( props, auth);      
088        
089        Transport transport = session.getTransport("smtp");                
090        if(hasAuth)transport.connect( host , username ,password );                
091        else transport.connect( ); 
092        boolean rtn=transport.isConnected();
093        transport.close();
094        
095        return rtn;
096    }
097}