Email with JavaMail copy

0

I have a system that sends email, it works normally, I want to insert a field to send copy of the email, a CC or CCo. here's a snippet of my code below.

Entity

    private String destino;
private String titulo;
private String mensagem;

Method sendsEmail

 public static void enviaEmail(Ordens ordens) throws EmailException {
     Email email = new SimpleEmail();
     email = conectaEmail();
     email.setSubject(ordens.getTitulo());
     email.setMsg(ordens.getMensagem());
     email.addTo(ordens.getDestino());
     String resposta = email.send();
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "E-mail enviado com sucesso para: " + ordens.getUsuario().getLogin(), "Informação"));
 }

Front

 <p:panel header="Envio de e-mail">
 <h:panelGrid columns="2">
 <h:outputText value="Para: "/>
 <h:inputText value="#{ordensControle.ordens.destino}" size="48"/>
 <h:outputText value="Título: "/>
 <h:inputText value="#{ordensControle.ordens.titulo}" size="48"/>
 <h:outputText value="Mensagem: "/>
 <h:inputTextarea value="#{ordensControle.ordens.mensagem}" cols="88" 
 rows="10"/>
 </h:panelGrid>
 <p:commandButton value="Enviar e-mail" icon="ui-icon-mail-closed"  
 onclick="PF('statusDialog').show();" action="#
 {ordensControle.enviaEmail()}" update="outputPanelMail"/>
 </p:panel>

Library Used, commons-email

    
asked by anonymous 16.05.2017 / 13:07

1 answer

1

Use the addBcc () method for hidden copy and addcc () for copying follows the reference below.

BCC: link

CC: link

In your code could be as follows for CC.

Entity

private String destino;
private String titulo;
private String mensagem;
private List<String> cc;

Method sendsEmail

public static void enviaEmail(Ordens ordens) throws EmailException {
     Email email = new SimpleEmail();
     email = conectaEmail();
     email.setSubject(ordens.getTitulo());
     email.setMsg(ordens.getMensagem());
     email.addTo(ordens.getDestino());
     for(String cc : ordens.getCc()){
         email.addCc(cc);
     }
     String resposta = email.send();
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "E-mail enviado com sucesso para: " + ordens.getUsuario().getLogin(), "Informação"));
 }
    
17.05.2017 / 12:42