Ola amigos,
eu botei um init() em um servlet e mandei ele executar quando inicia o tomcat, nesse init() eu coloquei a chamada do método que envia emails.
O problema é que quando o método executa, ou seja, quando inicio o tomcat ele envia só uma vez o email e depois não envia mais os emails quando eu solicito o envio. Tirando essa chamada no init() ele envia emails normalmente mas quando eu coloco essa função para iniciar com o tomcat, não funciona mais.
Função init() que está no servlet é assim
[code]public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException {
super.init(config);
try {
String msgSubject = "AVISO DO SISTEMA - SCC4";
String msgConteudo = "<br><br><b> TESTE</b> ";
ArrayList<String> listaEmail = new ArrayList<String>();
int idEmpresa = 24;
ArrayList<String> emailUs = Rh.controle.contrUsuariossistema.consultaEmailUsuarios(idEmpresa);
for (int j = 0; j < emailUs.size(); j++) {
listaEmail.add(emailUs.get(j));
}
//chama a funcao que envia o email
EnviaEmail.sendSimpleMail(msgSubject, listaEmail, msgConteudo);
} catch (Exception es) {
es.printStackTrace();
}
}
}[/code]
E a função que envia emails é essa:
[code]
public class EnviaEmail {
public static void sendSimpleMail ( String subject, ArrayList to, String mensagem) throws AddressException {
String mailServer = “mail.orga.com.br”;
String from = “info@scc4.com.br”;
if(to.size() > 0){
try{
Properties props = new Properties();
props.put(“mail.smtp.host”, mailServer);
props.put(“mail.smtp.auth”, “true”);
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(false);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
//Email com apenas Um Endereço(Recipient)
//InternetAddress addressTo = new InternetAddress(to);
//msg.setRecipient(Message.RecipientType.TO, addressTo);
//Email com Varios Endereços(Recipients)
InternetAddress[] addressTo = new InternetAddress[to.size()];
for (int i = 0; i < to.size(); i++) {
addressTo[i] = new InternetAddress(to.get(i));
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(mensagem, "text/html");
Transport.send(msg);
}
catch(SendFailedException ex ){
//logs(ex.toString(), ex.getStackTrace(), "Erro ao Enviar Email() ->> TO: " + to + " FROM: " + from);
}
catch(MessagingException ex ){
//logs(ex.toString(), ex.getStackTrace(), "Erro ao Enviar Email() ->> TO: " + to + " FROM: " + from);
}
catch(Exception ex ){
JOptionPane.showMessageDialog(null, "ERRO");
//logs(ex.toString(), ex.getStackTrace(), "Erro ao Enviar Email() ->> TO: " + to + " FROM: " + from);
}
}
}
public static class SMTPAuthenticator extends javax.mail.Authenticator{
@Override
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("info@scc4.com.br", "xxxxxxxx");
}
}
}[/code]
Se algue puder me dá uma luz,
valeu.