apex代码发来的日文邮件断弦了,如何编码?

发布于 2024-10-01 08:05:49 字数 1462 浏览 9 评论 0原文

我写了 Visualforce 页面,您可以发送电子邮件。它在英语中工作得很好。 但是当我在电子邮件正文部分输入日语并发送时, 我的电子邮件收件箱有问号而不是日语。

gmail 中的正文显示在所有 ?

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

我想我需要对字符串进行编码?但是如何在 Apex 代码中做到这一点呢? EncodeUtil 类有很少的方法,但它不使用 String 进行编码。

代码

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 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();
            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-10-08 08:05:49

我找到了解决方案,所以让我分享...

Messaging.SingleEmailMessage 类具有函数 setCharset()

所以在我问题的代码中,我只需要提供日语编码 <代码>“SHIFT-JIS”

mail.setCharset('Shift-JIS');

解决了:)

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 :)

满栀 2024-10-08 08:05:49

您应该将其设置为使用 UTF8 而不是 UTF-8,这样您就不会仅限于日语。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文