2011-04-25 18 views
13

Estoy usando el correo electrónico de Apache Commons para enviar correos electrónicos a mis clientes, pero tengo un cliente llamado 'Semana da Computação' (en portugués BR) pero dice 'Semana da Computação'. trato de modificar mi código, pero nada de trabajo:¿Cómo cambiar el juego de caracteres en Apache Commons?

public static boolean emailWithImage(String subject, String message, String emailReceiver, String imageURL) { 
    HtmlEmail email = new HtmlEmail(); 
    email.setCharset("UTF-8"); // I change here, but it is not working 
    email.setHostName(Constantes.EMAIL_HOST_NAME); 
    email.setSmtpPort(587); 
    DefaultAuthenticator authenticator = 
      new DefaultAuthenticator(Constantes.EMAIL_USER, Constantes.EMAIL_PASSWORD); 
    email.setAuthenticator(authenticator); 
    email.setTLS(true); 

    try { 
     email.setFrom(Constantes.EMAIL_USER, Constantes.EMAIL_NAME); 
     email.setSubject(subject); 
     email.addTo(emailReceiver); 

     URL url = new URL(imageURL); 
     String cid = email.embed(url, "image");  /* it must be 'cid' the name of the image */ 

     email.setHtmlMsg("<html><img src=\"cid:" + cid + "\"> <p>" + message + "</p> </html>"); /* set the html message */ 
     email.setTextMsg(message);      /* send a alternative text in case when the email reader don't support html */ 

     email.send(); 
    } catch (EmailException ex) { 
     return false; 
    } catch (MalformedURLException ex) { 
     return false; 
    } 

    return true; 
} 

¿Alguna idea? ¿Por qué el nombre no aparece correctamente y cómo puedo solucionarlo?

Respuesta

3

Podría funcionar si en lugar de hacer setHtmlMessage (...), lo hace

email.addPart("<html>body here</html>", "text/html;charset=UTF-8"); 

Otra alternativa es tratar de poner el juego de caracteres en el html,

email.setHtmlMsg("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"></head>body here</html>"); 
+0

Comprobé que la solución addPart funciona para mi uso. –

21

Esto también funciona ,

email.setCharset("utf-8"); 

Alternativamente, si usted está utilizando 1.3,

email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8); 
+0

Vea también http://stackoverflow.com/questions/6399187/apache-commons-email-and-utf-8 – nylund

1

Tuve el mismo problema, y ​​para resolverlo, configuré la codificación ISO-8859-1 y ahora está funcionando.

+1

Hola usuario, puede mostrar un ejemplo de código para que esta sea una respuesta más completa. ¡Buena suerte y espero que esto ayude! – jmort253

Cuestiones relacionadas