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);
}
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();
}
}
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;
}
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;
}
Aggregations