apex代码发来的日文邮件断弦了,如何编码?
我写了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案,所以让我分享...
Messaging.SingleEmailMessage
类具有函数setCharset()
所以在我问题的代码中,我只需要提供日语编码 <代码>“SHIFT-JIS”
解决了:)
I found the solution so let me share...
Messaging.SingleEmailMessage
class has functionsetCharset()
so in the code in my question, I just needed to provide Japanese encoding
"SHIFT-JIS"
Solved :)
您应该将其设置为使用 UTF8 而不是
UTF-8
,这样您就不会仅限于日语。You should set it to use UTF8 instead
UTF-8
so you won't be restricted to just Japanese.