1

When I am trying to send mail with the Japanese template through email message code, I am getting ??????? in the subject line instead of Japanese characters.

The template works fine when I use it through 'Send Test and Verify Merge Fields' button on the template. I am using 'Unicode (UTF-8)' as encoding and tried others like JIS and EUC but no luck.

Below is the code sending the mail. Please let me know if you need any more details

private static void sendEmailToUsers(List<User> listUsers,Id whatId, Map<String, Id> mapEmailTemplate, String[] ccAddress, String emailTemplate) {

    List<Account> a = new List<Account>();
    a = accMap.values();

    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>(); //created a list of SingleEmailMessage to collect all mails and send them in one go
    List<Messaging.SingleEmailMessage> mailnottosendList = new List<Messaging.SingleEmailMessage>();
    if(listUsers!=null && listUsers.size()>0){
        for(User userObject:listUsers){
            //Added below empty check for BUG-00137847 - Archana 
            if(mapEmailTemplate!=null && mapEmailTemplate.containsKey(emailTemplate +' '+ userObject.LanguageLocaleKey)){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
                String[] toaddress1 = new String[]{};
                toaddress1.add(userObject.Email);
                mail.setOrgWideEmailAddressId(System.Label.L10N_EmailLebel);
                mail.setToAddresses(toaddress1);
                mail.setReplyTo(System.Label.L10N_ReplyTo);
                mail.setTemplateId(Id.valueOf(mapEmailTemplate.get(emailTemplate +' '+ userObject.LanguageLocaleKey)));

                if(ccAddress!=null && ccAddress.size()>0){
                    mail.setCcAddresses(ccAddress);
                }
                //changing the targetobjectId from user to contact
                system.debug('a[0].Contact__c + '+a[0]);
                system.debug('a[0].Contact__c + '+a[0].Contact__c);
                mail.setTargetObjectId(a[0].Contact__c);
                //System.debug('--BKz--2--'+whatId);
                mail.setWhatId(whatId);
                mail.setSaveAsActivity(false);
                mailnottosendList.add(mail);
                //   mailList.add(mail); //adding all mails to the list
                system.debug('mail-----1>'+mail);
            }
        }
    }
    Savepoint sp = Database.setSavepoint();
    Messaging.sendEmail(mailnottosendList);
    Database.rollback(sp);
    for(Messaging.SingleEmailMessage email : mailnottosendList){
        Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
        emailToSend.setToAddresses(email.getToAddresses());
        emailToSend.setPlainTextBody(email.getPlainTextBody());
        emailToSend.setHTMLBody(email.getHTMLBody());
        emailToSend.setSubject(email.getSubject());
        emailToSend.setCcAddresses(email.getCcAddresses());
        String[] bccAddress = new String[]{'orion@f-erhypla240c7rrmhxzgmu33p9z3pirf3jqz7x56y9eigyftft.4b-cwrpuac.cs51.apex.sandbox.salesforce.com'};
        emailToSend.setBccAddresses(bccAddress); 
        emailToSend.setOrgWideEmailAddressId(System.Label.L10N_EmailLebel);
        emailToSend.setReplyTo(System.Label.L10N_ReplyTo);
        emailToSend.setSaveAsActivity(false);
        mailList.add(emailToSend);
        system.debug('emailToSend-----1>'+emailToSend);
    }


    for(Integer j = 0; j < mailList.size(); j++ ){
        allMails.add(mailList.get(j));
    }

}
6
  • The mail is being sent in the line below Savepoint sp = Database.setSavepoint();
    – Sam R
    Commented Jun 9, 2017 at 3:33
  • Your template MUST use Arial Unicode. It's the only one that supports double-byte Japanese characters.
    – crmprogdev
    Commented Jun 9, 2017 at 6:13
  • Can you please guide me on how can I use Arial Unicode.
    – Sam R
    Commented Jun 9, 2017 at 9:21
  • I would like to also add that the template works fine with the 'send test and verify merge fields' button on template.
    – Sam R
    Commented Jun 9, 2017 at 9:26
  • emailToSend.setSubject(email.getSubject()); This line sets the subject from the email template. The template specifies the font for each HTML email that's being sent. There could be an issue with the way the subject is being set in the Template. The template builder (assuming visualforce) is where you'd specify the font.
    – crmprogdev
    Commented Jun 9, 2017 at 11:19

1 Answer 1

2

I did some search and found that on User Detail page the 'Email Encoding' field should be set to "Unicode (UTF-8)" for the functionality to work fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .