}
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="";
//int _port;
// username/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;
synchronized(LOCK) {
try {
msgSess = createMimeMessage(config,server.getHostName(),server.getPort(),_username,_password,_tls,_ssl);
} 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);
sender.start();
SystemUtil.wait(lock, _timeout);
if(!sender.hasSended()) {
Throwable t = sender.getThrowable();
if(t!=null) throw Caster.toPageException(t);
// stop when still running
try{
if(sender.isAlive())sender.stop();
}
catch(Throwable t2){}
// after thread s stopped check send flag again
if(!sender.hasSended()){
throw new MessagingException("timeout occurred after "+(_timeout/1000)+" seconds while sending mail message");
}
}
clean(config,attachmentz);
listener(config,server,log,null,System.nanoTime()-start);
break;
}
catch (Exception e) {e.printStackTrace();
if(i+1==servers.length) {
listener(config,server,log,e,System.nanoTime()-start);
MailException me = new MailException(server.getHostName()+" "+LogUtil.toMessage(e)+":"+i);
me.setStackTrace(e.getStackTrace());
throw me;
}
}
}