Commit e2cc875ab57b556479bb8f5162010e454bdb2a44

Authored by fabricio
1 parent 2358f3b3
Exists in master

nova classe email.java para implementar contato

Showing 1 changed file with 122 additions and 0 deletions   Show diff stats
src/br/com/model/utilities/Email.java 0 → 100644
... ... @@ -0,0 +1,122 @@
  1 +package br.com.model.utilities;
  2 +
  3 +import java.io.IOException;
  4 +import java.net.MalformedURLException;
  5 +import java.util.Date;
  6 +import java.util.Properties;
  7 +
  8 +import lombok.AccessLevel;
  9 +import lombok.Getter;
  10 +import net.htmlparser.jericho.Source;
  11 +
  12 +import org.apache.commons.mail.EmailAttachment;
  13 +import org.apache.commons.mail.EmailException;
  14 +import org.apache.commons.mail.HtmlEmail;
  15 +import org.apache.commons.mail.MultiPartEmail;
  16 +
  17 +
  18 +/**
  19 + * @category utilities
  20 + * @version 1.0*/
  21 +public class Email {
  22 +
  23 + @Getter private String authUser;
  24 + @Getter private String authPass;
  25 + @Getter private String host;
  26 + @Getter private String auth;
  27 + @Getter private int port;
  28 + @Getter private String to;
  29 +
  30 + private MultiPartEmail email;
  31 + private EmailAttachment anexo;
  32 +
  33 +
  34 + private void initConf(String path){
  35 + ManagerProperties managerProperties = new ManagerProperties();
  36 + try {
  37 + Properties prop = managerProperties.getProp(path);
  38 +
  39 + this.host = prop.getProperty("prop.email.host");
  40 + this.port = Integer.parseInt(prop.getProperty("prop.email.port"));
  41 + this.authUser = prop.getProperty("prop.email.authuser");
  42 + this.authPass = prop.getProperty("prop.email.authpass");
  43 + this.auth = prop.getProperty("prop.email.auth");
  44 + this.to = prop.getProperty("prop.email.to");
  45 +
  46 + } catch (IOException e) {
  47 + // TODO Auto-generated catch block
  48 + e.printStackTrace();
  49 + }
  50 +
  51 + }
  52 +
  53 + /**
  54 + * @param String host, int port, String authUser, String authPass*/
  55 + public Email(String host, int port, String authUser, String authPass, String auth, String to){
  56 + this.host = host;
  57 + this.port = port;
  58 + this.authUser = authUser;
  59 + this.authPass = authPass;
  60 + this.auth = auth;
  61 + this.to = to;
  62 + this.email = new HtmlEmail();
  63 +
  64 + }
  65 +
  66 + /**
  67 + * @param String path. Caminho do arquivo mail.properties*/
  68 + public Email(String path) {
  69 + this.initConf(path);
  70 + this.email = new MultiPartEmail();
  71 + }
  72 +
  73 + /**
  74 + * @param String nomeDestinatario, String emailDestinatario, String nomeRemetente, String emailRemetente, String assunto, String mensagem
  75 + * @throws EmailException
  76 + * @throws MalformedURLException
  77 + * @return String*/
  78 + public String sendEmail( String nomeDestinatario, String emailDestinatario, String nomeRemetente, String emailRemetente, String assunto, String mensagem, boolean isHtml) throws EmailException {
  79 +
  80 + this.email.setHostName(this.host); // o servidor SMTP para envio do e-mail
  81 + this.email.addTo(emailDestinatario, nomeDestinatario); //destinat�rio
  82 + this.email.setFrom(emailRemetente, emailRemetente+", "); // remetente
  83 + this.email.setSubject(assunto); // assunto do e-mail
  84 +
  85 + if(isHtml)
  86 + this.email.setContent("<p>Ol�, <b>"+nomeRemetente+"</b> enviou a seguinte mensagem:</p> \n "+mensagem, "text/html");
  87 + else
  88 + this.email.setMsg("<p>Ol�, <b>"+nomeRemetente+"</b> enviou a seguinte mensagem:</p> \n "+mensagem); //conteudo do e-mail
  89 +
  90 + this.email.setSentDate(new Date());
  91 +
  92 + //this.email.setAuthentication(this.authUser, this.authPass);
  93 +
  94 +
  95 +
  96 + this.email.setSmtpPort(this.port);
  97 + this.email.setSSL(true);
  98 + this.email.setTLS(true);
  99 +
  100 + try {
  101 + this.email.send();
  102 + return "";
  103 +
  104 + } catch (Exception e) {
  105 + return e.getMessage();
  106 + }
  107 + }
  108 +
  109 + /**
  110 + * @param String pathArquivo, String descricao, String nome
  111 + * @return void
  112 + * @exception EmailException*/
  113 + public void addAnexo(String pathArquivo, String descricao, String nome) throws EmailException{
  114 + this.anexo = new EmailAttachment();
  115 + this.anexo.setPath(pathArquivo); //caminho do arquivo (RAIZ_PROJETO/teste/teste.txt)
  116 + this.anexo.setDisposition(EmailAttachment.ATTACHMENT);
  117 + this.anexo.setDescription(descricao);
  118 + this.anexo.setName(nome);
  119 + this.email.attach(anexo);
  120 + }
  121 +
  122 +}
0 123 \ No newline at end of file
... ...