Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.edu.cesmac.sic.core.domain.util; |
| 2 | |||
| 3 | import java.io.File; |
||
| 4 | import java.io.FileNotFoundException; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.util.ArrayList; |
||
| 7 | import java.util.Date; |
||
| 8 | import java.util.List; |
||
| 9 | import java.util.Properties; |
||
| 10 | |||
| 11 | import javax.activation.DataHandler; |
||
| 12 | import javax.mail.Address; |
||
| 13 | import javax.mail.MessagingException; |
||
| 14 | import javax.mail.Multipart; |
||
| 15 | import javax.mail.PasswordAuthentication; |
||
| 16 | import javax.mail.Session; |
||
| 17 | import javax.mail.Transport; |
||
| 18 | import javax.mail.internet.InternetAddress; |
||
| 19 | import javax.mail.internet.MimeBodyPart; |
||
| 20 | import javax.mail.internet.MimeMessage; |
||
| 21 | import javax.mail.internet.MimeMessage.RecipientType; |
||
| 22 | import javax.mail.internet.MimeMultipart; |
||
| 23 | import javax.mail.util.ByteArrayDataSource; |
||
| 24 | |||
| 25 | import org.apache.commons.mail.EmailException; |
||
| 26 | import org.apache.commons.mail.HtmlEmail; |
||
| 27 | |||
| 28 | import br.edu.cesmac.core.exception.NegocioException; |
||
| 29 | import br.edu.cesmac.core.util.ArquivoUtil; |
||
| 30 | import br.edu.cesmac.core.util.EmailBuilder; |
||
| 31 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 32 | import br.edu.cesmac.sic.core.domain.model.Arquivo; |
||
| 33 | import br.edu.cesmac.sic.core.domain.model.ProjetoPesquisaRelatorio; |
||
| 34 | |||
| 35 | public class EmailUtil { |
||
| 36 | |||
| 37 | private static final String MENSAGEM_TEXTO_SOBRE_MUDANCAS_SITUACAO_INSCRICAO_SISTEMA_INICIACAO_CIENTIFICA = "Houve atualização da situação da inscrição do projeto de pesquisa submetido. Para mais detalhes acesse o Sistema de Iniciação Científica."; |
||
| 38 | private static final String ASSUNTO_EMAIL_SISTEMA_INICIACAO_CIENTIFICA = "SISTEMA DE INICIAÇÃO CIENTÍFICA"; |
||
| 39 | private static final String NOME_PROPRIEDADE_EMAIL_EMAILORIGEM = "email.emailorigem"; |
||
| 40 | private static final String NOME_PROPRIEDADE_EMAIL_SENHA = "email.senha"; |
||
| 41 | private static final String NOME_PROPRIEDADE_EMAIL_USUARIO = "email.usuario"; |
||
| 42 | private static final String NOME_PROPRIEDADE_EMAIL_PORTA = "email.porta"; |
||
| 43 | private static final String NOME_PROPRIEDADE_EMAIL_HOSTNAME = "email.hostname"; |
||
| 44 | private static final String NOME_ARQUIVO_EMAIL_PROPERTY = "/email.properties"; |
||
| 45 | private static final String NOME_ARQUIVO_PADRAO_EMAIL = "/mensagemPadrao.html"; |
||
| 46 | |||
| 47 | |||
| 48 | public static void enviarEmailComInformacoesSobreInscricao(String emailDestino, String observacao) { |
||
| 49 | try { |
||
| 50 | HtmlEmail email = criarEmailHtmlIhAdicionarPropriedadePadrao(); |
||
| 51 | |||
| 52 | email.addTo(emailDestino); |
||
| 53 | email.setSubject(ASSUNTO_EMAIL_SISTEMA_INICIACAO_CIENTIFICA); |
||
| 54 | // String textoHtml = gerarHtmlMensagemDoEmail(usuario, senha); |
||
| 55 | // email.setHtmlMsg(textoHtml.toString()); |
||
| 56 | // email.setHtmlMsg(observacao); |
||
| 57 | StringBuilder texto = new StringBuilder(MENSAGEM_TEXTO_SOBRE_MUDANCAS_SITUACAO_INSCRICAO_SISTEMA_INICIACAO_CIENTIFICA); |
||
| 58 | if (VerificadorUtil.naoEstaNuloOuVazio(observacao)) { |
||
| 59 | texto.append("\n\nOBSERVAÇÕES:\n"); |
||
| 60 | texto.append(observacao); |
||
| 61 | } |
||
| 62 | email.setTextMsg(texto.toString()); |
||
| 63 | email.send(); |
||
| 64 | |||
| 65 | } catch (EmailException | IOException e) { |
||
| 66 | throw new NegocioException("Erro ao enviar email: "+ e.getMessage()); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | public static void enviarEmailComInformacoesSobreInscricao(String emailDestino, String mensagemPrincipal, String observacao) { |
||
| 71 | try { |
||
| 72 | HtmlEmail email = criarEmailHtmlIhAdicionarPropriedadePadrao(); |
||
| 73 | |||
| 74 | email.addTo(emailDestino); |
||
| 75 | email.setSubject(ASSUNTO_EMAIL_SISTEMA_INICIACAO_CIENTIFICA); |
||
| 76 | StringBuilder texto = new StringBuilder(mensagemPrincipal); |
||
| 77 | if (VerificadorUtil.naoEstaNuloOuVazio(observacao)) { |
||
| 78 | texto.append("\n\nOBSERVAÇÕES:\n"); |
||
| 79 | texto.append(observacao); |
||
| 80 | } |
||
| 81 | email.setTextMsg(texto.toString()); |
||
| 82 | email.send(); |
||
| 83 | |||
| 84 | } catch (EmailException | IOException e) { |
||
| 85 | throw new NegocioException("Erro ao enviar email: "+ e.getMessage()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | public static void enviarEmailParaContatoProjetoPesquisa(String emailDestino, String tituloProjetoPesquisa, String conteudo) { |
||
| 90 | try { |
||
| 91 | HtmlEmail email = criarEmailHtmlIhAdicionarPropriedadePadrao(); |
||
| 92 | |||
| 93 | email.addTo(emailDestino); |
||
| 94 | email.setSubject(ASSUNTO_EMAIL_SISTEMA_INICIACAO_CIENTIFICA + " : " + tituloProjetoPesquisa); |
||
| 95 | // email.setHtmlMsg(conteudo); |
||
| 96 | email.setTextMsg(conteudo); |
||
| 97 | email.send(); |
||
| 98 | |||
| 99 | } catch (EmailException | IOException e) { |
||
| 100 | throw new NegocioException("Erro ao enviar email: "+ e.getMessage()); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | public static void enviarEmailRelatorioFinal(String emailDestino, ProjetoPesquisaRelatorio projetoPesquisaRelatorio, String legendaArquivo, Arquivo arquivo) { |
||
| 105 | try { |
||
| 106 | final PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 107 | |||
| 108 | MimeMessage email = new EmailBuilder(propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME), |
||
| 109 | propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA), |
||
| 110 | propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM), |
||
| 111 | propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_SENHA)) |
||
| 112 | .comEmailOrigem(new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY).retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM)) |
||
| 113 | .comDataEnvio(new Date()) |
||
| 114 | .comEmailDestino(emailDestino) |
||
| 115 | .comAssunto(projetoPesquisaRelatorio.getProjetoPesquisa().getDescricaoDoProcessoSeletivo()) |
||
| 116 | .comConteudoHtml("<html>Código HTML da mensagem ") |
||
| 117 | .adicionarAnexo(legendaArquivo, arquivo.getArquivo(), arquivo.getExtensao()) |
||
| 118 | .build(); |
||
| 119 | Transport.send(email); |
||
| 120 | } catch (Exception e) { |
||
| 121 | e.printStackTrace(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | private static Session criarSessaoPropriedadePadrao() throws IOException, EmailException { |
||
| 126 | try { |
||
| 127 | final PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 128 | Properties props = new Properties(); |
||
| 129 | props.put("mail.smtp.host", propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME)); |
||
| 130 | props.put("mail.smtp.auth", "true"); |
||
| 131 | props.put("mail.smtp.port", propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA)); |
||
| 132 | props.put("mail.smtp.starttls.enable", "true"); |
||
| 133 | props.put("mail.smtp.socketFactory.port", propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA)); |
||
| 134 | props.put("mail.smtp.socketFactory.fallback", "false"); |
||
| 135 | props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
||
| 136 | |||
| 137 | return Session.getDefaultInstance(props, new javax.mail.Authenticator() { |
||
| 138 | protected PasswordAuthentication getPasswordAuthentication() { |
||
| 139 | try { |
||
| 140 | return new PasswordAuthentication(propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM), propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_SENHA)); |
||
| 141 | } catch (IOException e) { |
||
| 142 | e.printStackTrace(); |
||
| 143 | } |
||
| 144 | return null; |
||
| 145 | } |
||
| 146 | }); |
||
| 147 | } catch (Exception e) { |
||
| 148 | e.printStackTrace(); |
||
| 149 | } |
||
| 150 | return null; |
||
| 151 | } |
||
| 152 | |||
| 153 | /* |
||
| 154 | private static String gerarHtmlMensagemDoEmail(String usuario, String senha) { |
||
| 155 | AbrirArquivo abrirArquivo = new AbrirArquivo(); |
||
| 156 | return abrirArquivo.retornarConteudoDoArquivo(NOME_ARQUIVO_PADRAO_EMAIL); |
||
| 157 | } |
||
| 158 | */ |
||
| 159 | |||
| 160 | private static HtmlEmail criarEmailHtmlIhAdicionarPropriedadePadrao() throws IOException, EmailException { |
||
| 161 | PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 162 | |||
| 163 | String hostname = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME); |
||
| 164 | String porta = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA); |
||
| 165 | String usuarioEmail =propertiesUtil.retornarPropriedadeDoArquivo( NOME_PROPRIEDADE_EMAIL_USUARIO); |
||
| 166 | String senha = propertiesUtil.retornarPropriedadeDoArquivo( NOME_PROPRIEDADE_EMAIL_SENHA); |
||
| 167 | String emailOrigem = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM); |
||
| 168 | |||
| 169 | HtmlEmail email = new HtmlEmail(); |
||
| 170 | email.setHostName(hostname); |
||
| 171 | email.setSmtpPort(new Integer(porta)); |
||
| 172 | email.setAuthentication(usuarioEmail, senha); |
||
| 173 | email.setSSLOnConnect(true); |
||
| 174 | email.setFrom(emailOrigem); |
||
| 175 | return email; |
||
| 176 | } |
||
| 177 | |||
| 178 | public static void enviarEmailComParecerDoRecurso(String emailDestino, String conteudo, Object object) { |
||
| 179 | try { |
||
| 180 | HtmlEmail email = criarEmailHtmlIhAdicionarPropriedadePadrao(); |
||
| 181 | |||
| 182 | email.addTo(emailDestino); |
||
| 183 | email.setSubject(ASSUNTO_EMAIL_SISTEMA_INICIACAO_CIENTIFICA + " : PARECER"); |
||
| 184 | email.setTextMsg(conteudo); |
||
| 185 | email.send(); |
||
| 186 | |||
| 187 | } catch (EmailException | IOException e) { |
||
| 188 | throw new NegocioException("Erro ao enviar email: "+ e.getMessage()); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | public static class MailSenderPlus { |
||
| 193 | //o tratamento de excessoes foi simplificado (throws MessagingException) |
||
| 194 | //para facilitar o entendimento do exemplo |
||
| 195 | public static void main(String[] args) throws MessagingException, FileNotFoundException, IOException { |
||
| 196 | /* |
||
| 197 | String hostname = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME); |
||
| 198 | String porta = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_PORTA); |
||
| 199 | String usuarioEmail =propertiesUtil.retornarPropriedadeDoArquivo( NOME_PROPRIEDADE_EMAIL_USUARIO); |
||
| 200 | String senha = propertiesUtil.retornarPropriedadeDoArquivo( NOME_PROPRIEDADE_EMAIL_SENHA); |
||
| 201 | String emailOrigem = propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM); |
||
| 202 | |||
| 203 | HtmlEmail email = new HtmlEmail(); |
||
| 204 | email.setHostName(hostname); |
||
| 205 | email.setSmtpPort(new Integer(porta)); |
||
| 206 | email.setAuthentication(usuarioEmail, senha); |
||
| 207 | email.setSSLOnConnect(true); |
||
| 208 | email.setFrom(emailOrigem); |
||
| 209 | */ |
||
| 210 | //objeto para definicao das propriedades de configuracao do provider |
||
| 211 | // PropertiesUtil propertiesUtil = new PropertiesUtil(NOME_ARQUIVO_EMAIL_PROPERTY); |
||
| 212 | Properties props = new Properties(); |
||
| 213 | //o valor "smtp.host.com.br" deve ser alterado para o seu servidor SMTP |
||
| 214 | // props.put("mail.host",propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_HOSTNAME)); |
||
| 215 | |||
| 216 | props.put("mail.smtp.host", "smtp.gmail.com"); |
||
| 217 | props.put("mail.smtp.auth", "true"); |
||
| 218 | props.put("mail.smtp.port", "465"); |
||
| 219 | props.put("mail.smtp.starttls.enable", "true"); |
||
| 220 | props.put("mail.smtp.socketFactory.port", "465"); |
||
| 221 | props.put("mail.smtp.socketFactory.fallback", "false"); |
||
| 222 | props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
||
| 223 | //obtendo um Session com a configuração |
||
| 224 | //do servidor SMTP definida em props |
||
| 225 | Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { |
||
| 226 | protected PasswordAuthentication getPasswordAuthentication() { |
||
| 227 | return new PasswordAuthentication("brunolopespeixoto@gmail.com", "bruno0910"); |
||
| 228 | } |
||
| 229 | }); |
||
| 230 | |||
| 231 | //criando a mensagem |
||
| 232 | MimeMessage message = new MimeMessage(session); |
||
| 233 | |||
| 234 | //substituir pelos e-mails desejados |
||
| 235 | // Address from = new InternetAddress(propertiesUtil.retornarPropriedadeDoArquivo(NOME_PROPRIEDADE_EMAIL_EMAILORIGEM)); |
||
| 236 | Address from = new InternetAddress("brunolopespeixoto@gmail.com"); |
||
| 237 | Address to = new InternetAddress("brunolopespeixoto@hotmail.com"); |
||
| 238 | |||
| 239 | //configurando o remetente e o destinatario |
||
| 240 | message.setFrom(from); |
||
| 241 | message.addRecipient(RecipientType.TO, to); |
||
| 242 | |||
| 243 | //configurando a data de envio, o assunto e o texto da mensagem |
||
| 244 | message.setSentDate(new Date()); |
||
| 245 | message.setSubject("Enviando uma mensagem formatada com anexo"); |
||
| 246 | |||
| 247 | // conteudo html que sera atribuido a mensagem |
||
| 248 | String htmlMessage = "< h t m l > Código HTML da mensagem "; |
||
| 249 | |||
| 250 | //criando a Multipart |
||
| 251 | Multipart multipart = new MimeMultipart(); |
||
| 252 | |||
| 253 | //criando a primeira parte da mensagem |
||
| 254 | MimeBodyPart attachment0 = new MimeBodyPart(); |
||
| 255 | //configurando o htmlMessage com o mime type |
||
| 256 | attachment0.setContent(htmlMessage,"text/html; charset=UTF-8"); |
||
| 257 | //adicionando na multipart |
||
| 258 | multipart.addBodyPart(attachment0); |
||
| 259 | |||
| 260 | //arquivo que será anexado |
||
| 261 | String pathname = "C:/Users/bruno/Downloads/teste.jpeg";//pode conter o caminho |
||
| 262 | File file = new File(pathname); |
||
| 263 | |||
| 264 | // ByteArrayDataSource dataSource = new ByteArrayDataSource(arquivo.getArquivo(), ArquivoUtil.retornarContentTypeArquivo(arquivo.getExtensao())); |
||
| 265 | ByteArrayDataSource dataSource = new ByteArrayDataSource(new Arquivo().getArquivo(), ""); |
||
| 266 | |||
| 267 | //criando a segunda parte da mensagem |
||
| 268 | MimeBodyPart attachment1 = new MimeBodyPart(); |
||
| 269 | //configurando o DataHandler para o arquivo desejado |
||
| 270 | //a leitura dos bytes, descoberta e configuracao do tipo |
||
| 271 | //do arquivo serão resolvidos pelo JAF (DataHandler e FileDataSource) |
||
| 272 | // attachment1.setDataHandler(new DataHandler(new FileDataSource(file))); |
||
| 273 | attachment1.setDataHandler(new DataHandler(dataSource)); |
||
| 274 | //configurando o nome do arquivo que pode ser diferente do arquivo |
||
| 275 | //original Ex: setFileName("outroNome.png") |
||
| 276 | // attachment1.setFileName(file.getName()); |
||
| 277 | attachment1.setFileName("legendaArquivo"); |
||
| 278 | //adicionando o anexo na multipart |
||
| 279 | multipart.addBodyPart(attachment1); |
||
| 280 | |||
| 281 | //adicionando a multipart como conteudo da mensagem |
||
| 282 | message.setContent(multipart); |
||
| 283 | |||
| 284 | //enviando |
||
| 285 | Transport.send(message); |
||
| 286 | |||
| 287 | System.out.println("E-mail enviado com sucesso!"); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | } |