简体   繁体   中英

Java send email with Japanese character

I am using java to send emails. requirement is to send email in different languages, for Japanese I get email as "???????" characters.

The code is something like this:

import java.io.IOException;
import static java.nio.charset.StandardCharsets.*;
import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import java.io.File;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Service
public class EmailService {

    @Autowired(required=true)
    private JavaMailSenderImpl javaMailSender;

    void sendEmailhtml(String to, String sub, String body, String imageFile) throws MessagingException, IOException {
        javaMailSender = new JavaMailSenderImpl();

    javaMailSender.setHost("smtp.test.com");
        MimeMessage msg = javaMailSender.createMimeMessage();
        msg.setHeader("Content-Type", "text/html; charset=UTF-8");
        msg.setContent("Content-Type", "text/html; charset=UTF-8");

        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setTo(to);
        byte[] bytes = sub.getBytes();
        sub = new String(bytes, UTF_8);
        helper.setSubject(sub);
        byte[] bytesBody = body.getBytes();
        body = new String(bytesBody, UTF_8);
        helper.setText(body,true);
        // add image file as inline image
        if(imageFile!= null) {
            FileSystemResource fileresource = new FileSystemResource(new File(imageFile));
            helper.addInline("referral-email-image", fileresource);
        }
        helper.setFrom("test@invalidemail.com");
        javaMailSender.send(msg);
    }

}

I am calling sendEmailhtml function with below argument value.

body=<p style="align-content: center;"> <img alt="referral emai header" src="cid:referral-email-image"/> </p>  <p>FirstNameさん, </p>     <p>CandidateFName CandidateLastName さんの社員紹介プロセスが完了しました.CandidateFNameさんは候補者プロフィールを作成され、現在、採用担当者がレビューを実施中です。<b>CandidateFName</b> さんに連絡を差し上げ、当社の <b> <a alt="Careers Site" href="https://test.com/">キャリアサイト</a></b> へのリンクから他の求人にも応募可能であることもご案内しました。 </p>    <p>         <p>            今回の推薦について重ねて御礼申し上げます。今後、あなたから当人に連絡をしていただく必要はありません。選考プロセスを進めるかどうかについて、採用担当者より <b>CandidateFName</b>さんに直接連絡を取らせていただきます。
 
sub=あなたが紹介した候補者に関する最新情報

Any assistance provided would be greatly appreciated.

I think your problem is in the line byte[] bytes = sub.getBytes(); This line gets you bytes of the string in the charset that is default on your system which may not be UTF-8. change it to
byte[] bytes = sub.getBytes(StandardCharsets.UTF_8);
This will first convert String into UTF-8 and then return the byte array that represent your String content as UTF-8 encoded as oppose to encoded into your system default charset. So then when you read your bytes with line

body = new String(bytesBody, UTF_8);

You should get a desired result.
Also here is the tool that helped me a lot to diagnose and debug thorny encoding issues. There is an Open Source java library MgntUtils (written by me) that has a Utility that converts Strings to unicode sequence and vise versa:

result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);

The output of this code is:

\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
Hello World

The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM