001    package railo.runtime.net.smtp;
002    
003    import javax.mail.Transport;
004    
005    import railo.commons.io.SystemUtil;
006    import railo.runtime.net.smtp.SMTPClient.MimeMessageAndSession;
007    
008    
009    public final class SMTPSender extends Thread {
010    
011            private boolean hasSended=false;
012            private Throwable throwable;
013            private Object lock;
014            private String host;
015            private int port;
016            private String user;
017            private String pass;
018            private MimeMessageAndSession mmas;
019            
020            public SMTPSender(Object lock, MimeMessageAndSession mmas, String host, int port, String user, String pass) {
021                    this.lock=lock;
022                    this.mmas=mmas;
023    
024                    this.host=host;
025                    this.port=port;
026                    this.user=user;
027                    this.pass=pass;
028            }
029            
030            @Override
031            public void run() {
032                    Transport transport = null;
033            try {
034                    transport = mmas.session.transport;//SMTPConnectionPool.getTransport(session,host,port,user,pass);
035                    if(user==null)pass=null;
036                    // connect
037                    if(!transport.isConnected())
038                            transport.connect(host,port,user,pass);
039    
040                    
041                            mmas.message.saveChanges();  
042                            transport.sendMessage(mmas.message, mmas.message.getAllRecipients());
043                            hasSended=true;
044                    } 
045                    catch (Throwable t) {
046                            this.throwable=t;
047                    }
048                    finally {
049                            try {SMTPConnectionPool.releaseSessionAndTransport(mmas.session);}catch (Throwable t) {}
050                            SystemUtil.notify(lock);
051                    }
052            }
053    
054            /**
055             * @return the messageExpection
056             */
057            public Throwable getThrowable() {
058                    return throwable;
059            }
060    
061            /**
062             * @return is message sended
063             */
064            public boolean hasSended() {
065                    return hasSended;
066            }
067    
068    }