Search in sources :

Example 1 with MailException

use of lucee.runtime.net.mail.MailException in project Lucee by lucee.

the class SMTPClient method send.

public void send(PageContext pc, long sendTime) throws MailException {
    if (plainText == null && htmlText == null)
        throw new MailException("you must define plaintext or htmltext");
    Server[] servers = ((PageContextImpl) pc).getMailServers();
    ConfigWeb config = pc.getConfig();
    if (ArrayUtil.isEmpty(servers) && ArrayUtil.isEmpty(host))
        throw new MailException("no SMTP Server defined");
    if (spool == SPOOL_YES || (spool == SPOOL_UNDEFINED && config.isMailSpoolEnable())) {
        MailSpoolerTask mst = new MailSpoolerTask(this, servers, sendTime);
        if (listener instanceof Component)
            mst.setListener(new ComponentSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (Component) listener));
        else if (listener instanceof UDF)
            mst.setListener(new UDFSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (UDF) listener));
        config.getSpoolerEngine().add(mst);
    } else
        _send(config, servers);
}
Also used : Server(lucee.runtime.net.mail.Server) UDFSpoolerTaskListener(lucee.runtime.spooler.UDFSpoolerTaskListener) UDF(lucee.runtime.type.UDF) MailException(lucee.runtime.net.mail.MailException) PageContextImpl(lucee.runtime.PageContextImpl) MailSpoolerTask(lucee.runtime.spooler.mail.MailSpoolerTask) Component(lucee.runtime.Component) ConfigWeb(lucee.runtime.config.ConfigWeb) ComponentSpoolerTaskListener(lucee.runtime.spooler.ComponentSpoolerTaskListener)

Example 2 with MailException

use of lucee.runtime.net.mail.MailException in project Lucee by lucee.

the class SMTPClient method _send.

public void _send(lucee.runtime.config.ConfigWeb config, Server[] servers) throws MailException {
    long start = System.nanoTime();
    long _timeout = getTimeout(config);
    try {
        Proxy.start(proxyData);
        Log log = ((ConfigImpl) config).getLog("mail");
        // Server[] servers = config.getMailServers();
        if (host != null) {
            int prt;
            String usr, pwd;
            ServerImpl[] nServers = new ServerImpl[host.length];
            for (int i = 0; i < host.length; i++) {
                usr = null;
                pwd = null;
                prt = Server.DEFAULT_PORT;
                if (port > 0)
                    prt = port;
                if (!StringUtil.isEmpty(username)) {
                    usr = username;
                    pwd = password;
                }
                nServers[i] = toServerImpl(host[i], prt, usr, pwd, lifeTimespan, idleTimespan);
                if (ssl == SSL_YES)
                    nServers[i].setSSL(true);
                if (tls == TLS_YES)
                    nServers[i].setTLS(true);
            }
            servers = nServers;
        }
        if (servers.length == 0) {
            // return;
            throw new MailException("no SMTP Server defined");
        }
        boolean _ssl, _tls;
        for (int i = 0; i < servers.length; i++) {
            Server server = servers[i];
            String _username = null, _password = "";
            if (server.hasAuthentication()) {
                _username = server.getUsername();
                _password = server.getPassword();
            }
            // tls
            if (tls != TLS_NONE)
                _tls = tls == TLS_YES;
            else
                _tls = ((ServerImpl) server).isTLS();
            // ssl
            if (ssl != SSL_NONE)
                _ssl = ssl == SSL_YES;
            else
                _ssl = ((ServerImpl) server).isSSL();
            MimeMessageAndSession msgSess;
            boolean recyleConnection = ((ServerImpl) server).reuseConnections();
            {
                // synchronized(LOCK) {
                try {
                    msgSess = createMimeMessage(config, server.getHostName(), server.getPort(), _username, _password, ((ServerImpl) server).getLifeTimeSpan(), ((ServerImpl) server).getIdleTimeSpan(), _tls, _ssl, ((ConfigImpl) config).isMailSendPartial(), !recyleConnection, ((ConfigImpl) config).isUserset());
                } catch (MessagingException e) {
                    // listener
                    listener(config, server, log, e, System.nanoTime() - start);
                    MailException me = new MailException(e.getMessage());
                    me.setStackTrace(e.getStackTrace());
                    throw me;
                }
                try {
                    SerializableObject lock = new SerializableObject();
                    SMTPSender sender = new SMTPSender(lock, msgSess, server.getHostName(), server.getPort(), _username, _password, recyleConnection);
                    sender.start();
                    SystemUtil.wait(lock, _timeout);
                    if (!sender.isSent()) {
                        Throwable t = sender.getThrowable();
                        if (t != null)
                            throw Caster.toPageException(t);
                        // stop when still running
                        try {
                            if (sender.isAlive())
                                sender.stop();
                        } catch (Throwable t2) {
                            ExceptionUtil.rethrowIfNecessary(t2);
                        }
                        // after thread is stopped check sent flag again
                        if (!sender.isSent()) {
                            throw new MessagingException("timeout occurred after " + (_timeout / 1000) + " seconds while sending mail message");
                        }
                    }
                    // could have an exception but was send anyway
                    if (sender.getThrowable() != null) {
                        Throwable t = sender.getThrowable();
                        if (log != null)
                            LogUtil.log(log, Log.LEVEL_ERROR, "send mail", t);
                    }
                    clean(config, attachmentz);
                    listener(config, server, log, null, System.nanoTime() - start);
                    break;
                } catch (Exception e) {
                    SystemOut.printDate(e);
                    if (i + 1 == servers.length) {
                        listener(config, server, log, e, System.nanoTime() - start);
                        MailException me = new MailException(server.getHostName() + " " + ExceptionUtil.getStacktrace(e, true) + ":" + i);
                        me.setStackTrace(e.getStackTrace());
                        throw me;
                    }
                }
            }
        }
    } finally {
        Proxy.end();
    }
}
Also used : Server(lucee.runtime.net.mail.Server) Log(lucee.commons.io.log.Log) MessagingException(javax.mail.MessagingException) MessagingException(javax.mail.MessagingException) PageException(lucee.runtime.exp.PageException) FileNotFoundException(java.io.FileNotFoundException) MailException(lucee.runtime.net.mail.MailException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) SerializableObject(lucee.commons.lang.SerializableObject) ServerImpl(lucee.runtime.net.mail.ServerImpl) MailException(lucee.runtime.net.mail.MailException) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 3 with MailException

use of lucee.runtime.net.mail.MailException in project Lucee by lucee.

the class Mail method doEndTag.

@Override
public int doEndTag() throws PageException {
    if (listener == null) {
        ApplicationContextSupport acs = (ApplicationContextSupport) pageContext.getApplicationContext();
        listener = acs.getMailListener();
    }
    if (listener != null)
        smtp.setListener(listener);
    smtp.setTimeZone(pageContext.getTimeZone());
    try {
        smtp.send(pageContext, sendTime != null ? sendTime.getTime() : 0);
    } catch (MailException e) {
        throw Caster.toPageException(e);
    }
    return EVAL_PAGE;
}
Also used : ApplicationContextSupport(lucee.runtime.listener.ApplicationContextSupport) MailException(lucee.runtime.net.mail.MailException)

Example 4 with MailException

use of lucee.runtime.net.mail.MailException in project Lucee by lucee.

the class SMTPClient method toServerImpl.

public static ServerImpl toServerImpl(String server, int port, String usr, String pwd, long lifeTimespan, long idleTimespan) throws MailException {
    int index;
    // username/password
    index = server.indexOf('@');
    if (index != -1) {
        usr = server.substring(0, index);
        server = server.substring(index + 1);
        index = usr.indexOf(':');
        if (index != -1) {
            pwd = usr.substring(index + 1);
            usr = usr.substring(0, index);
        }
    }
    // port
    index = server.indexOf(':');
    if (index != -1) {
        try {
            port = Caster.toIntValue(server.substring(index + 1));
        } catch (ExpressionException e) {
            throw new MailException(e.getMessage());
        }
        server = server.substring(0, index);
    }
    ServerImpl srv = ServerImpl.getInstance(server, port, usr, pwd, lifeTimespan, idleTimespan, false, false);
    return srv;
}
Also used : ServerImpl(lucee.runtime.net.mail.ServerImpl) MailException(lucee.runtime.net.mail.MailException) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

MailException (lucee.runtime.net.mail.MailException)4 ExpressionException (lucee.runtime.exp.ExpressionException)2 Server (lucee.runtime.net.mail.Server)2 ServerImpl (lucee.runtime.net.mail.ServerImpl)2 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MessagingException (javax.mail.MessagingException)1 Log (lucee.commons.io.log.Log)1 SerializableObject (lucee.commons.lang.SerializableObject)1 Component (lucee.runtime.Component)1 PageContextImpl (lucee.runtime.PageContextImpl)1 ConfigImpl (lucee.runtime.config.ConfigImpl)1 ConfigWeb (lucee.runtime.config.ConfigWeb)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 PageException (lucee.runtime.exp.PageException)1 ApplicationContextSupport (lucee.runtime.listener.ApplicationContextSupport)1 ComponentSpoolerTaskListener (lucee.runtime.spooler.ComponentSpoolerTaskListener)1 UDFSpoolerTaskListener (lucee.runtime.spooler.UDFSpoolerTaskListener)1 MailSpoolerTask (lucee.runtime.spooler.mail.MailSpoolerTask)1 UDF (lucee.runtime.type.UDF)1