Guest User

Untitled

a guest
Sep 23rd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.28 KB | None | 0 0
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import javax.mail.internet.AddressException;
  13. import javax.mail.internet.InternetAddress;
  14.  
  15. import org.apache.commons.mail.EmailAttachment;
  16. import org.apache.commons.mail.EmailException;
  17. import org.apache.commons.mail.HtmlEmail;
  18.  
  19. public class enviarEmail {
  20.    
  21.     public enviarEmail() throws EmailException, MalformedURLException {
  22.         conectar();
  23.     }
  24.  
  25.     public void enviaEmailFormatoHtml(Connection conexao,
  26.             Integer intemailid, String assunto, String mensagem, Integer intenvioid
  27.     ) throws EmailException, MalformedURLException {
  28.         String path = "/home/cedecon/www/email2/_lib/file/";
  29.         Integer var_qtd_sucesso = 0;
  30.         Integer var_qtd_erro = 0;
  31.         Integer cont = 0;
  32.  
  33.    
  34.        
  35.        
  36.         try{
  37.            
  38. //        Statement sqlassinantes = conexao.createStatement();  
  39.        
  40.         PreparedStatement sqlassinantes = conexao.prepareStatement(wizardQuery,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  41.        
  42.  
  43.         ResultSet rsassinantes = sqlassinantes.executeQuery("select tblassinante.intassinanteid as intassinanteid, tblassinante.stremail as stremail from tblemaillista " +
  44.                 " inner join tblassinante on tblassinante.intlistaemailid = tblemaillista.intlistaid " +
  45.                 " inner join tblenvio on tblenvio.intemailid = tblemaillista.intemailid " +
  46.                 " where " +
  47.                 " tblassinante.bolativo = 1 " +
  48.                 " and " +
  49.                 " intenvioid = " + intenvioid.toString() +
  50.                 " and " +
  51.                 " tblassinante.intassinanteid not in ( " +
  52.                 " select intassinanteid from tblenvioassinante where intenvioid = " + intenvioid.toString() +
  53.                 " ) " );
  54.        
  55.        
  56.         while (rsassinantes.next()) {
  57. //            HtmlEmail email2 = email;
  58.             List<InternetAddress> emailTo = new ArrayList();
  59.             emailTo.add(new InternetAddress(rsassinantes.getString("stremail")));
  60.             //email2.addTo(rsassinantes.getString("stremail"));
  61.             //email.addTo(rsassinantes.getString("stremail")); //destinatário
  62.            
  63.             cont = cont + 1;
  64.            
  65.             if ( (!rsassinantes.hasNext() ) OR (cont == 25) )
  66.             {
  67.                 HtmlEmail email = new HtmlEmail();
  68.                 String msgembed = "";
  69.                 cont = 0;
  70.                 try {
  71.                     Statement sqlanexo = conexao.createStatement();  
  72.                    
  73.                     ResultSet rsanexo = sqlanexo.executeQuery("select intemailanexoid, intemailid, strnomearquivo, bolanexo from tblemailanexo where intemailid = " + intemailid.toString() + " order by bolanexo");
  74.  
  75.                     while (rsanexo.next()) {
  76.                         String arquivonome = path + "doc/anexos/" + intemailid + "/" + rsanexo.getString("strnomearquivo");
  77.                         System.out.println(arquivonome);
  78.                         arquivonome = "/home/thiago/Imagens/avatar161.jpg";
  79.                         Integer bolanexo = rsanexo.getInt("bolanexo");
  80.                         System.out.println("anexos");
  81.                        
  82.                         if (bolanexo == 1)
  83.                         {
  84.                             EmailAttachment anexo1 = new EmailAttachment();
  85.                             anexo1.setPath(arquivonome); //caminho do arquivo (RAIZ_PROJETO/teste/teste.txt)
  86.                             anexo1.setDisposition(EmailAttachment.ATTACHMENT);
  87.                             anexo1.setDescription(arquivonome);
  88.                             anexo1.setName(arquivonome);        
  89.                             email.attach(anexo1);
  90. //                            anexo1.destroy;
  91.                         }
  92.                         else
  93.                         {
  94.                             msgembed = msgembed + "<img src=\"cid:" + email.embed(new URL("file:" + arquivonome), arquivonome) + "\"><br>";
  95.                         }
  96.                     }
  97.  
  98.                 } catch(SQLException e) {
  99.                     System.out.println("SQL Exception... Erro na consulta de anexos:");
  100.                     e.printStackTrace();
  101.                 }
  102.                
  103.                 // configura a mensagem para o formato HTML
  104.                 email.setHtmlMsg("<html>" + mensagem + "<br>" + msgembed + " </html>");
  105.  
  106.                 email.setTextMsg("Seu servidor de e-mail não suporta mensagem HTML");
  107.                 System.out.println("configuracao do email");
  108.                
  109.                 email.setSubject(assunto); // assunto do e-mail
  110.                 email.setHostName("mail.cedecon.net"); // o servidor SMTP para envio do e-mail
  111.                 email.setFrom("posgra@cedecon.net", "Cedecon"); // remetente
  112.                 email.setAuthentication("posgra@cedecon.net", "clkpca");
  113.                 email.setSmtpPort(587);
  114.                 email.setSSL(false);
  115.                 email.setDebug(false);
  116. //                email.setSSL(true);
  117. //                email.setTLS(true);
  118.                 // envia email
  119.                 email.setBcc(emailTo);
  120.                 try
  121.                 {
  122.                     email.send();
  123.                
  124.                 var_qtd_sucesso = var_qtd_sucesso + 1;
  125.                 } catch (EmailException e) {
  126.                     var_qtd_erro = var_qtd_erro + 1;
  127.                 }
  128.  
  129.             }
  130.            
  131.         }
  132.  
  133.     } catch(SQLException e) {
  134.         System.out.println("SQL Exception... Erro na consulta de assinantes:");
  135.         e.printStackTrace();
  136.     }
  137.     catch (AddressException e) {
  138.         // TODO Auto-generated catch block
  139.         e.printStackTrace();
  140.     }
  141.        
  142.        
  143. System.out.println("Email enviado com sucesso teste: " + var_qtd_sucesso.toString());
  144. System.out.println("erro ao enviar email teste: " + var_qtd_erro.toString());
  145.        
  146.        
  147.  
  148.     }
  149.        
  150.    
  151.     public void conectar() throws MalformedURLException, EmailException {
  152.         Connection conn = null;
  153.  
  154.             try {
  155.                 Class.forName("com.mysql.jdbc.Driver");
  156.                 conn = DriverManager.getConnection( "jdbc:mysql://localhost/dbemail","root","nana" );
  157.                 System.out.println("A conexão foi um sucesso\n");  
  158.                 Statement stm = conn.createStatement();  
  159.                
  160.                 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");
  161.                 while (rs.next()) {
  162.                    
  163.                     Integer intemailid = rs.getInt("intemailid");
  164.                     String strassunto = rs.getString("strassunto");
  165.                     String strmensagem = rs.getString("strmensagem");
  166.                     Integer intenvioid = rs.getInt("intenvioid");
  167.  
  168.                     enviaEmailFormatoHtml(conn, intemailid,strassunto,strmensagem,intenvioid);
  169.                    
  170.                 }
  171.                 System.out.println("\n\nConsulta realizada com sucesso!!!\n");
  172.            } catch(ClassNotFoundException e) {
  173.                 System.out.println("excessão Classe não encontrada");
  174.                 e.printStackTrace();
  175.             } catch(SQLException e) {
  176.                 System.out.println("SQL Exception... Erro na consulta:");
  177.                 e.printStackTrace();
  178.             } finally {
  179.                     try {
  180.                         conn.close();
  181.                     } catch (SQLException e) {
  182.                         // TODO Auto-generated catch block
  183.                         System.out.println("\n\nerro ao fechar a conexão");
  184.                     }
  185.                     System.out.println("\n\nFechamendo a conexão");
  186.             }
  187.                
  188.             }
  189.            
  190.     /**
  191.      * @param args
  192.      * @throws EmailException
  193.      * @throws MalformedURLException
  194.      */
  195.     public static void main(String[] args) throws EmailException, MalformedURLException {
  196.         new enviarEmail();
  197.        
  198.     }
  199.  
  200. }
Add Comment
Please, Sign In to add comment