Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import org.apache.commons.mail.EmailAttachment;
- import org.apache.commons.mail.EmailException;
- import org.apache.commons.mail.HtmlEmail;
- public class enviarEmail {
- public enviarEmail() throws EmailException, MalformedURLException {
- conectar();
- }
- public void enviaEmailFormatoHtml(Connection conexao,
- Integer intemailid, String assunto, String mensagem, Integer intenvioid
- ) throws EmailException, MalformedURLException {
- String path = "/home/cedecon/www/email2/_lib/file/";
- Integer var_qtd_sucesso = 0;
- Integer var_qtd_erro = 0;
- Integer cont = 0;
- try{
- // Statement sqlassinantes = conexao.createStatement();
- PreparedStatement sqlassinantes = conexao.prepareStatement(wizardQuery,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- ResultSet rsassinantes = sqlassinantes.executeQuery("select tblassinante.intassinanteid as intassinanteid, tblassinante.stremail as stremail from tblemaillista " +
- " inner join tblassinante on tblassinante.intlistaemailid = tblemaillista.intlistaid " +
- " inner join tblenvio on tblenvio.intemailid = tblemaillista.intemailid " +
- " where " +
- " tblassinante.bolativo = 1 " +
- " and " +
- " intenvioid = " + intenvioid.toString() +
- " and " +
- " tblassinante.intassinanteid not in ( " +
- " select intassinanteid from tblenvioassinante where intenvioid = " + intenvioid.toString() +
- " ) " );
- while (rsassinantes.next()) {
- // HtmlEmail email2 = email;
- List<InternetAddress> emailTo = new ArrayList();
- emailTo.add(new InternetAddress(rsassinantes.getString("stremail")));
- //email2.addTo(rsassinantes.getString("stremail"));
- //email.addTo(rsassinantes.getString("stremail")); //destinatário
- cont = cont + 1;
- if ( (!rsassinantes.hasNext() ) OR (cont == 25) )
- {
- HtmlEmail email = new HtmlEmail();
- String msgembed = "";
- cont = 0;
- try {
- Statement sqlanexo = conexao.createStatement();
- ResultSet rsanexo = sqlanexo.executeQuery("select intemailanexoid, intemailid, strnomearquivo, bolanexo from tblemailanexo where intemailid = " + intemailid.toString() + " order by bolanexo");
- while (rsanexo.next()) {
- String arquivonome = path + "doc/anexos/" + intemailid + "/" + rsanexo.getString("strnomearquivo");
- System.out.println(arquivonome);
- arquivonome = "/home/thiago/Imagens/avatar161.jpg";
- Integer bolanexo = rsanexo.getInt("bolanexo");
- System.out.println("anexos");
- if (bolanexo == 1)
- {
- EmailAttachment anexo1 = new EmailAttachment();
- anexo1.setPath(arquivonome); //caminho do arquivo (RAIZ_PROJETO/teste/teste.txt)
- anexo1.setDisposition(EmailAttachment.ATTACHMENT);
- anexo1.setDescription(arquivonome);
- anexo1.setName(arquivonome);
- email.attach(anexo1);
- // anexo1.destroy;
- }
- else
- {
- msgembed = msgembed + "<img src=\"cid:" + email.embed(new URL("file:" + arquivonome), arquivonome) + "\"><br>";
- }
- }
- } catch(SQLException e) {
- System.out.println("SQL Exception... Erro na consulta de anexos:");
- e.printStackTrace();
- }
- // configura a mensagem para o formato HTML
- email.setHtmlMsg("<html>" + mensagem + "<br>" + msgembed + " </html>");
- email.setTextMsg("Seu servidor de e-mail não suporta mensagem HTML");
- System.out.println("configuracao do email");
- email.setSubject(assunto); // assunto do e-mail
- email.setHostName("mail.cedecon.net"); // o servidor SMTP para envio do e-mail
- email.setFrom("posgra@cedecon.net", "Cedecon"); // remetente
- email.setAuthentication("posgra@cedecon.net", "clkpca");
- email.setSmtpPort(587);
- email.setSSL(false);
- email.setDebug(false);
- // email.setSSL(true);
- // email.setTLS(true);
- // envia email
- email.setBcc(emailTo);
- try
- {
- email.send();
- var_qtd_sucesso = var_qtd_sucesso + 1;
- } catch (EmailException e) {
- var_qtd_erro = var_qtd_erro + 1;
- }
- }
- }
- } catch(SQLException e) {
- System.out.println("SQL Exception... Erro na consulta de assinantes:");
- e.printStackTrace();
- }
- catch (AddressException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Email enviado com sucesso teste: " + var_qtd_sucesso.toString());
- System.out.println("erro ao enviar email teste: " + var_qtd_erro.toString());
- }
- public void conectar() throws MalformedURLException, EmailException {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection( "jdbc:mysql://localhost/dbemail","root","nana" );
- System.out.println("A conexão foi um sucesso\n");
- Statement stm = conn.createStatement();
- ResultSet rs = stm.executeQuery("select * from tblemail inner join tblenvio on tblenvio.intemailid = tblemail.intemailid where bolenviado = 0 and dtaagendamento <= now() and tblenvio.bolativo = 1");
- while (rs.next()) {
- Integer intemailid = rs.getInt("intemailid");
- String strassunto = rs.getString("strassunto");
- String strmensagem = rs.getString("strmensagem");
- Integer intenvioid = rs.getInt("intenvioid");
- enviaEmailFormatoHtml(conn, intemailid,strassunto,strmensagem,intenvioid);
- }
- System.out.println("\n\nConsulta realizada com sucesso!!!\n");
- } catch(ClassNotFoundException e) {
- System.out.println("excessão Classe não encontrada");
- e.printStackTrace();
- } catch(SQLException e) {
- System.out.println("SQL Exception... Erro na consulta:");
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- System.out.println("\n\nerro ao fechar a conexão");
- }
- System.out.println("\n\nFechamendo a conexão");
- }
- }
- /**
- * @param args
- * @throws EmailException
- * @throws MalformedURLException
- */
- public static void main(String[] args) throws EmailException, MalformedURLException {
- new enviarEmail();
- }
- }
Add Comment
Please, Sign In to add comment