E-mail with Javamail copy

Asked

Viewed 524 times

0

I have a system that sends e-mail, works normally, I want to insert a field to send copies of email, a CC or Cco. follows below an excerpt of my code.

Entity

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

Sent method

 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>

Ultilizada Library , Commons-email

  • Put in your question the email library you used, I believe it is apache Commons email

1 answer

1


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

BCC: https://commons.apache.org/proper/commons-email/apidocs/org/apache/commons/mail/Email.html#addBcc(java.lang.String)

CC: https://commons.apache.org/proper/commons-email/apidocs/org/apache/commons/mail/Email.html#addCc(java.lang.String)

In your code could look like this for CC.

Entity

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

Sent method

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"));
 }
  • Great guy, I’ll edit the question to see if you can help me in the Front-End as it will go from there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.