Pergunta

I wrote visualforce page that you can send email. It works fine in English. But when I typed Japanese in email body section and send it, my email inbox has question marks instead of Japanese.

body text in gmail shows up in all ?

??????????????????????????

I think I need to encode the string? But how to do it in Apex code? EncodeUtil class has few methods but it does not take String for encoding.

code

public PageReference sendEmail() {
    Messaging.SingleEmailMessage mail = new Messaging.singleEmailMessage();
            //subject
            subject = 'my subject';
            mail.setSubject(subject);
            //set sender name
            mail.setSenderDisplayName('im sender');
            //set recipient
            emailTo = 'test@test.com'; //test sample email address
            mail.setToAddresses(new String[]{emailTo});
            //set body
                String bodyText = '送信者'; //add Japanese to body
            mail.setPlainTextBody(bodyText);

            try{
                Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                if(resultMail[0].isSuccess())
                    response = 'ok sent!';
                else{
                    response = resultMail[0].getErrors().get(0).getMessage();
                }
            }catch(System.EmailException ex){
                response = ex.getMessage();
            }
}
Foi útil?

Solução

I found the solution so let me share...

Messaging.SingleEmailMessage class has function setCharset()

so in the code in my question, I just needed to provide Japanese encoding "SHIFT-JIS"

mail.setCharset('Shift-JIS');

Solved :)

Outras dicas

You should set it to use UTF8 instead UTF-8 so you won't be restricted to just Japanese.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top