无法使用日语字符对主题进行编码

发布于 2025-01-05 19:00:30 字数 998 浏览 1 评论 0原文

我有一个带有主题和内容字段的jsp表单,它接受用户的输入。程序发出一封邮件。这里两个字段都设置为 String 对象。发送邮件后,日语或中文字符将成为垃圾字符。如果主题为“英语”,则工作正常。

的代码

// create a message
    MimeMessage msg = new MimeMessage(session);
if(subject != null)

        msg.setSubject(subject, "UTF-8");
        //msg.setSubject(subject);//commented to take care of email

    // create the Multipart and add its parts to it
    //MimeMultipart mp = new MimeMultipart();
    MimeMultipart mp = new MimeMultipart("alternative");

    if(body != null)
    {
        // create and fill the first message part

        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(body);
        mbp1.setHeader("Content-Type","text/html;charset=utf-8");
        mbp1.setHeader("Content-Transfer-Encoding", "quoted-printable");
        //mbp1.setContent(body, "text/html");
        mp.addBodyPart(mbp1);
    }

这是我在主题字段中将主题指定为汉字测试消息并发送邮件 。我得到的日语字符是垃圾字符。所以我收到的消息主题为 ?测试消息

但当我收到邮件时,主题中仍然收到一些垃圾字符。

I have a jsp form with subject, content fields which accepts input from user. The program sends out a mail. Here both the fields are set as String objects. After sending mail characters in japanese or chinese are coming as junk characters. If subject as "english" it is working fine.

This is my code

// create a message
    MimeMessage msg = new MimeMessage(session);
if(subject != null)

        msg.setSubject(subject, "UTF-8");
        //msg.setSubject(subject);//commented to take care of email

    // create the Multipart and add its parts to it
    //MimeMultipart mp = new MimeMultipart();
    MimeMultipart mp = new MimeMultipart("alternative");

    if(body != null)
    {
        // create and fill the first message part

        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(body);
        mbp1.setHeader("Content-Type","text/html;charset=utf-8");
        mbp1.setHeader("Content-Transfer-Encoding", "quoted-printable");
        //mbp1.setContent(body, "text/html");
        mp.addBodyPart(mbp1);
    }

I had given subject as 漢字 test message in the subject field and sending mail. I am getting junk characters for japanese characters. So I am getting message subject as ?? test message

But still getting some junk characters in subject when I get mail.

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

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

发布评论

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

评论(1

自我难过 2025-01-12 19:00:30

主题字符串是如何创建的?发送表单数据时是否考虑了用户浏览器使用的字符编码?您是否验证该字符串包含正确的 Unicode 字符数据?

How was the subject string created? Did it take into account the character encoding used by the user's browser when sending the form data? Did you verify that the string contains the correct Unicode character data?

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