Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 303 | espaco | 1 | package br.com.ec.domain.shared; |
| 2 | |||
| 3 | import java.util.Date; |
||
| 4 | import java.util.Properties; |
||
| 5 | |||
| 6 | import javax.activation.DataHandler; |
||
| 7 | import javax.mail.Message; |
||
| 8 | import javax.mail.Multipart; |
||
| 9 | import javax.mail.PasswordAuthentication; |
||
| 10 | import javax.mail.Session; |
||
| 11 | import javax.mail.Transport; |
||
| 12 | import javax.mail.internet.InternetAddress; |
||
| 13 | import javax.mail.internet.MimeBodyPart; |
||
| 14 | import javax.mail.internet.MimeMessage; |
||
| 15 | import javax.mail.internet.MimeMessage.RecipientType; |
||
| 16 | import javax.mail.internet.MimeMultipart; |
||
| 17 | import javax.mail.util.ByteArrayDataSource; |
||
| 18 | |||
| 19 | import br.com.ec.core.util.ArquivoUtil; |
||
| 20 | import br.com.ec.core.util.PropertiesUtil; |
||
| 21 | import br.com.ec.core.util.VerificadorUtil; |
||
| 22 | |||
| 23 | public class GerenciadorEmailImpl { |
||
| 24 | |||
| 25 | private MimeMessage email; |
||
| 26 | private Multipart conteudo = new MimeMultipart(); |
||
| 27 | |||
| 28 | public GerenciadorEmailImpl() { |
||
| 29 | try { |
||
| 30 | PropertiesUtil propertiesUtil = new PropertiesUtil("/email/email.properties"); |
||
| 31 | String hostname = propertiesUtil.retornarPropriedadeDoArquivo("email.hostname"); |
||
| 32 | String porta = propertiesUtil.retornarPropriedadeDoArquivo("email.porta"); |
||
| 33 | final String emailOrigem = propertiesUtil.retornarPropriedadeDoArquivo("email.emailorigem"); |
||
| 34 | final String senha = propertiesUtil.retornarPropriedadeDoArquivo( "email.senha"); |
||
| 35 | |||
| 36 | Properties propriedades = new Properties(); |
||
| 37 | propriedades.put("mail.smtp.host", hostname); |
||
| 38 | propriedades.put("mail.smtp.auth", "true"); |
||
| 39 | propriedades.put("mail.smtp.port", porta); |
||
| 40 | |||
| 41 | email = new MimeMessage(Session.getDefaultInstance(propriedades, new javax.mail.Authenticator() { |
||
| 42 | protected PasswordAuthentication getPasswordAuthentication() { |
||
| 43 | return new PasswordAuthentication(emailOrigem, senha); |
||
| 44 | } |
||
| 45 | })); |
||
| 46 | this.comEmailOrigem(emailOrigem); |
||
| 47 | } catch (Exception e) { |
||
| 48 | e.printStackTrace(); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | public GerenciadorEmailImpl comSessao(Session sessao) { |
||
| 53 | email = new MimeMessage(sessao); |
||
| 54 | return this; |
||
| 55 | } |
||
| 56 | |||
| 57 | public GerenciadorEmailImpl comDataEnvio(Date dataEnvio) { |
||
| 58 | try { |
||
| 59 | email.setSentDate(dataEnvio); |
||
| 60 | } catch (Exception e) { |
||
| 61 | e.printStackTrace(); |
||
| 62 | } |
||
| 63 | return this; |
||
| 64 | } |
||
| 65 | |||
| 66 | public GerenciadorEmailImpl comEmailOrigem(String emailOrigem) { |
||
| 67 | try { |
||
| 68 | email.setFrom(new InternetAddress(emailOrigem)); |
||
| 69 | } catch (Exception e) { |
||
| 70 | e.printStackTrace(); |
||
| 71 | } |
||
| 72 | return this; |
||
| 73 | } |
||
| 74 | |||
| 75 | public GerenciadorEmailImpl comEmailDestino(String emailDestino) { |
||
| 76 | try { |
||
| 77 | email.setRecipient(RecipientType.TO, new InternetAddress(emailDestino)); |
||
| 78 | } catch (Exception e) { |
||
| 79 | e.printStackTrace(); |
||
| 80 | } |
||
| 81 | return this; |
||
| 82 | } |
||
| 83 | |||
| 84 | public GerenciadorEmailImpl comEmailsDestino(String emailsDestino) { |
||
| 85 | try { |
||
| 86 | email.setRecipients(RecipientType.TO, InternetAddress.parse(emailsDestino)); |
||
| 87 | } catch (Exception e) { |
||
| 88 | e.printStackTrace(); |
||
| 89 | } |
||
| 90 | return this; |
||
| 91 | } |
||
| 92 | |||
| 93 | public GerenciadorEmailImpl comAssunto(String assunto) { |
||
| 94 | try { |
||
| 95 | email.setSubject(assunto); |
||
| 96 | } catch (Exception e) { |
||
| 97 | e.printStackTrace(); |
||
| 98 | } |
||
| 99 | return this; |
||
| 100 | } |
||
| 101 | |||
| 102 | public GerenciadorEmailImpl comConteudo(String corpo) { |
||
| 103 | try { |
||
| 104 | conteudo = null; |
||
| 105 | email.setText(corpo); |
||
| 106 | } catch (Exception e) { |
||
| 107 | e.printStackTrace(); |
||
| 108 | } |
||
| 109 | return this; |
||
| 110 | } |
||
| 111 | |||
| 112 | public GerenciadorEmailImpl comConteudoHtml(String corpo) { |
||
| 113 | try { |
||
| 114 | MimeBodyPart corpoHTML = new MimeBodyPart(); |
||
| 115 | corpoHTML.setContent(corpo,"text/html; charset=UTF-8"); |
||
| 116 | conteudo.addBodyPart(corpoHTML); |
||
| 117 | } catch (Exception e) { |
||
| 118 | e.printStackTrace(); |
||
| 119 | } |
||
| 120 | return this; |
||
| 121 | } |
||
| 122 | |||
| 123 | public GerenciadorEmailImpl adicionarAnexo(String legendaArquivo, byte[] arquivoEmBytes, String contentTypeArquivo) { |
||
| 124 | try { |
||
| 125 | if (VerificadorUtil.estaNulo(conteudo)) { |
||
| 126 | conteudo = new MimeMultipart(); |
||
| 127 | } |
||
| 128 | ByteArrayDataSource dataSource = new ByteArrayDataSource(arquivoEmBytes, ArquivoUtil.retornarContentTypeArquivo(contentTypeArquivo)); |
||
| 129 | MimeBodyPart anexo = new MimeBodyPart(); |
||
| 130 | anexo.setDataHandler(new DataHandler(dataSource)); |
||
| 131 | anexo.setFileName(legendaArquivo); |
||
| 132 | conteudo.addBodyPart(anexo); |
||
| 133 | } catch (Exception e) { |
||
| 134 | e.printStackTrace(); |
||
| 135 | } |
||
| 136 | return this; |
||
| 137 | } |
||
| 138 | |||
| 139 | public final MimeMessage build() { |
||
| 140 | try { |
||
| 141 | if (VerificadorUtil.naoEstaNulo(conteudo)) { |
||
| 142 | email.setContent(conteudo); |
||
| 143 | } |
||
| 144 | } catch (Exception e) { |
||
| 145 | e.printStackTrace(); |
||
| 146 | } |
||
| 147 | return email; |
||
| 148 | } |
||
| 149 | |||
| 150 | public final void enviar() { |
||
| 151 | try { |
||
| 152 | Message message = new MimeMessage(this.build()); |
||
| 153 | Transport.send(message); |
||
| 154 | } catch (Exception e) { |
||
| 155 | e.printStackTrace(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /* |
||
| 160 | private static final String NOME_PROPRIEDADE_EMAIL_EMAILORIGEM = "email.emailorigem"; |
||
| 161 | private static final String NOME_PROPRIEDADE_EMAIL_SENHA = "email.senha"; |
||
| 162 | private static final String NOME_PROPRIEDADE_EMAIL_USUARIO = "email.usuario"; |
||
| 163 | private static final String NOME_PROPRIEDADE_EMAIL_PORTA = "email.porta"; |
||
| 164 | private static final String NOME_PROPRIEDADE_EMAIL_HOSTNAME = "email.hostname"; |
||
| 165 | private static final String NOME_ARQUIVO_EMAIL_PROPERTY = "/email/email.properties"; |
||
| 166 | |||
| 167 | private String assunto; |
||
| 168 | private String conteudo; |
||
| 169 | private String destinatarios; |
||
| 170 | |||
| 171 | private String nomeArquivo; |
||
| 172 | private byte[] arquivoAnexo; |
||
| 173 | private String contentTypeAnexo; |
||
| 174 | |||
| 175 | private static final String EMAIL_FROM = "sistema@espacocase.net"; |
||
| 176 | private static final String EMAIL_PASS = "espaco10"; |
||
| 177 | |||
| 178 | public String getAssunto() {return assunto;} |
||
| 179 | public void setAssunto(String assunto) {this.assunto = assunto;} |
||
| 180 | |||
| 181 | public String getConteudo() {return conteudo;} |
||
| 182 | public void setConteudo(String conteudo) {this.conteudo = conteudo;} |
||
| 183 | |||
| 184 | public String getDestinatarios() {return destinatarios;} |
||
| 185 | public void setDestinatarios(String destinatarios) {this.destinatarios = destinatarios;} |
||
| 186 | |||
| 187 | public String getNomeArquivo() {return nomeArquivo;} |
||
| 188 | public void setNomeArquivo(String nomeArquivo) {this.nomeArquivo = nomeArquivo;} |
||
| 189 | |||
| 190 | public byte[] getArquivoAnexo() {return arquivoAnexo;} |
||
| 191 | public void setArquivoAnexo(byte[] arquivoAnexo) {this.arquivoAnexo = arquivoAnexo;} |
||
| 192 | |||
| 193 | public String getContentTypeAnexo() {return contentTypeAnexo;} |
||
| 194 | public void setContentTypeAnexo(String contentTypeAnexo) {this.contentTypeAnexo = contentTypeAnexo;} |
||
| 195 | |||
| 196 | private Session configurandoEmail() { |
||
| 197 | try { |
||
| 198 | PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 199 | String hostname = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME); |
||
| 200 | String porta = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA); |
||
| 201 | final String emailOrigem = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM); |
||
| 202 | final String senha = propertiesUtil.retornarPropriedadeDoArquivo( NOME_PROPRIEDADE_EMAIL_SENHA); |
||
| 203 | |||
| 204 | Properties props = new Properties(); |
||
| 205 | props.put("mail.smtp.host", hostname); |
||
| 206 | props.put("mail.smtp.auth", "true"); |
||
| 207 | props.put("mail.smtp.port", porta); |
||
| 208 | |||
| 209 | System.out.println(emailOrigem + "s"); |
||
| 210 | System.out.println(emailOrigem.equals(EMAIL_FROM)); |
||
| 211 | System.out.println(senha.equals(EMAIL_PASS)); |
||
| 212 | |||
| 213 | Session sessao = Session.getDefaultInstance(props, new javax.mail.Authenticator() { |
||
| 214 | protected PasswordAuthentication getPasswordAuthentication() { |
||
| 215 | return new PasswordAuthentication(emailOrigem, senha); |
||
| 216 | } |
||
| 217 | }); |
||
| 218 | // sessao.setDebug(true); |
||
| 219 | return sessao; |
||
| 220 | } catch (IOException e) { |
||
| 221 | e.printStackTrace(); |
||
| 222 | } |
||
| 223 | return null; |
||
| 224 | } |
||
| 225 | |||
| 226 | public void enviarEmail() { |
||
| 227 | try { |
||
| 228 | PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 229 | String emailOrigem = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM); |
||
| 230 | |||
| 231 | Message message = new MimeMessage(configurandoEmail()); |
||
| 232 | message.setFrom(new InternetAddress(emailOrigem)); |
||
| 233 | |||
| 234 | Address[] emailsParaEnvio = InternetAddress.parse(destinatarios); |
||
| 235 | message.setRecipients(Message.RecipientType.TO, emailsParaEnvio); |
||
| 236 | message.setSubject(getAssunto()); |
||
| 237 | message.setText(getConteudo()); |
||
| 238 | |||
| 239 | if (VerificadorUtil.naoEstaNuloOuVazio(getArquivoAnexo())) { |
||
| 240 | Multipart conteudo = new MimeMultipart(); |
||
| 241 | ByteArrayDataSource dataSource = new ByteArrayDataSource(getArquivoAnexo(), ArquivoUtil.retornarContentTypeArquivo(getContentTypeAnexo())); |
||
| 242 | MimeBodyPart anexo = new MimeBodyPart(); |
||
| 243 | anexo.setDataHandler(new DataHandler(dataSource)); |
||
| 244 | anexo.setFileName(getNomeArquivo()); |
||
| 245 | conteudo.addBodyPart(anexo); |
||
| 246 | message.setContent(conteudo); |
||
| 247 | } |
||
| 248 | |||
| 249 | Transport.send(message); |
||
| 250 | } catch (MessagingException e) { |
||
| 251 | e.printStackTrace(); |
||
| 252 | } catch (IOException e) { |
||
| 253 | e.printStackTrace(); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | */ |
||
| 257 | |||
| 258 | /* |
||
| 259 | public static void main(String[] args) { |
||
| 260 | Properties props = new Properties(); |
||
| 261 | |||
| 262 | props.put("mail.smtp.host", "mail.espacocase.net"); |
||
| 263 | props.put("mail.smtp.auth", "true"); |
||
| 264 | props.put("mail.smtp.port", "587"); |
||
| 265 | |||
| 266 | Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { |
||
| 267 | protected PasswordAuthentication getPasswordAuthentication() { |
||
| 268 | return new PasswordAuthentication("sistema@espacocase.net", "espaco10"); |
||
| 269 | } |
||
| 270 | }); |
||
| 271 | |||
| 272 | session.setDebug(true); |
||
| 273 | |||
| 274 | try { |
||
| 275 | //Remetente |
||
| 276 | Message message = new MimeMessage(session); |
||
| 277 | // message.setFrom(new InternetAddress("espacocase@gmail.com")); |
||
| 278 | message.setFrom(new InternetAddress("sistema@espacocase.net")); |
||
| 279 | |||
| 280 | //Destinatário(s) |
||
| 281 | Address[] toUser = InternetAddress.parse(ConstantesSEC.DESTINATARIOS_EMAIL_DIRECAO); |
||
| 282 | |||
| 283 | message.setRecipients(Message.RecipientType.TO, toUser); |
||
| 284 | message.setSubject("ESPAÇO CASE - CONSOLIDAÇÃO DO DIA 12/05/2015");//Assunto |
||
| 285 | message.setText("CONSOLIDAÇÃO DO DIA 12/05/2015:\n\nValor bruto: R$13.045,00\nMeta: -R$16.750,00\nPendências: 1\n\nVendas do dia: R$1.600,00.\nLoja Poço: R$600,00.\nLoja Quiosque: R$1.000,00"); |
||
| 286 | |||
| 287 | // message.setSubject("ESPAÇO CASE E VOCÊ");//Assunto |
||
| 288 | // message.setText("OLÃ {Cliente}, chegaram novidades para o seu aparelho. Acesse: www.espacocase.com.br e confira!"); |
||
| 289 | Transport.send(message); |
||
| 290 | |||
| 291 | System.out.println("Feito!!!"); |
||
| 292 | } catch (MessagingException e) { |
||
| 293 | e.printStackTrace(); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | */ |
||
| 297 | } |