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 = '[email protected]'; //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();
}
}
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 :)