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.smtp;
020
021import javax.mail.Transport;
022
023import lucee.commons.io.SystemUtil;
024import lucee.commons.lang.ExceptionUtil;
025import lucee.runtime.net.smtp.SMTPClient.MimeMessageAndSession;
026
027
028public final class SMTPSender extends Thread {
029
030        private boolean isSent = false;
031        private Throwable throwable;
032        private Object lock;
033        private String host;
034        private int port;
035        private String user;
036        private String pass;
037        private MimeMessageAndSession mmas;
038        private final boolean recyleConnection;
039        
040        public SMTPSender(Object lock, MimeMessageAndSession mmas, String host, int port, String user, String pass, boolean reuseConnection) {
041                this.lock=lock;
042                this.mmas=mmas;
043
044                this.host=host;
045                this.port=port;
046                this.user=user;
047                this.pass=pass;
048                this.recyleConnection=reuseConnection;
049        }
050        
051        @Override
052        public void run() {
053                Transport transport = null;
054        try {
055                transport = mmas.session.transport;//SMTPConnectionPool.getTransport(session,host,port,user,pass);
056                if(user==null)pass=null;
057                // connect
058                if(!transport.isConnected())
059                        transport.connect(host,port,user,pass);
060                
061                        mmas.message.saveChanges();  
062                        transport.sendMessage(mmas.message, mmas.message.getAllRecipients());
063                        isSent = true;
064                } 
065                catch (Throwable t) {
066                        ExceptionUtil.rethrowIfNecessary(t);
067                        this.throwable=t;
068                }
069                finally {
070                        try {
071                                if(recyleConnection)SMTPConnectionPool.releaseSessionAndTransport(mmas.session);
072                                else SMTPConnectionPool.disconnect(mmas.session.transport);
073                                
074                        }catch (Throwable t) {
075                                ExceptionUtil.rethrowIfNecessary(t);
076                        }
077                        SystemUtil.notify(lock);
078                }
079        }
080
081        /**
082         * @return the messageExpection
083         */
084        public Throwable getThrowable() {
085                return throwable;
086        }
087
088        /**
089         * @return was message sent
090         */
091        public boolean isSent() {
092                return isSent;
093        }
094
095}