磅标志使用java.mail包在邮件内容中不起作用?

时间:2011-01-13 06:33:08

标签: java unicode javamail

全部,

我正在使用javax.mail packaage MINEMESSAGE,MimeMultipart类发送邮件,但即使我提到类型为utf-8,unicode字符也无法在正文中使用。像英镑标志不起作用。请帮忙做什么。这是我的邮件标题

To: kumar.k@mysite.com

Message-ID: <875158456.1.1294898905049.JavaMail.root@nextrelease>

Subject: My Site Free Trial - 5 days left

MIME-Version: 1.0
Content-Type: multipart/mixed; 
 boundary="----=_Part_0_1733237863.1294898905008"

MyHeaderName: myHeaderValue

Date: Thu, 13 Jan 2011 06:08:25 +0000 (UTC)

------=_Part_0_1733237863.1294898905008

Content-Type: text/html; charset=UTF-8

Content-Transfer-Encoding: quoted-printable

**Here Body message**


    Take advantage of our best value price  annual subscription at only= =EF=BF=BD=EF=BF=BD49    per month. Or our no commitment monthly subscription= at just =EF=BF=BD=EF=BF=BD59 per month. "

Actual out put should be: "Take advantage of our best value price  annual subscription at only £49 per month. Or our no commitment monthly subscription at just £59 per month.

以下是代码

MimeMessage msg = new MimeMessage(session);

        InternetAddress addressTo[] = new InternetAddress[emsg.getRecipeants()
                .size()];
        int i = 0;
        try {
            for (String email : emsg.getRecipeants()) {
                InternetAddress address = new InternetAddress(email);
                addressTo[i] = address;
                log.info(acc.getSenderEmailID() + " sending mail To " + email);
            }

            msg.setRecipients(Message.RecipientType.TO, addressTo);
            if (emsg.getReplayTO() != null) {
                msg.setReplyTo(new InternetAddress[] { new InternetAddress(emsg
                        .getReplayTO()) });
            }
            String from = "";
            if (!acc.getAccoutName().equals("")) {
                from = acc.getAccoutName() + "<" + acc.getSenderEmailID() + ">";
            } else {
                from = acc.getSenderEmailID();
            }
            msg.setFrom(new InternetAddress(from));
            // MultiPart body part

            Multipart multipart = new MimeMultipart();

            // This is the message body content part

            MimeBodyPart messageContentPart = new MimeBodyPart();

            String content = processContent(emsg.content, multipart,
                    comapanyDBName);
            if (emsg.isPlain) {
                messageContentPart.setContent(content,
                        "text/plain; charset=UTF-8");
            } else {
                messageContentPart.setContent(content,
                        "text/html; charset=UTF-8");
            }
            multipart.addBodyPart(messageContentPart);

            // This is the template Attachment part
            if (emsg.getAttachment() != null) {
                for (File file : emsg.getAttachment()) {
                    MimeBodyPart messageAttachmentBodyPart = new MimeBodyPart();
                    messageAttachmentBodyPart = new MimeBodyPart();

                    DataSource source = new FileDataSource(file);

                    messageAttachmentBodyPart.setDataHandler(new DataHandler(
                            source));
                    messageAttachmentBodyPart.setFileName(file.getName());
                    multipart.addBodyPart(messageAttachmentBodyPart);
                }
            }

            // Put parts in message
            msg.setContent(multipart);

            // end

            // Optional : You can also set your custom headers in the Email
            // if
            // you
            // Want
            msg.addHeader("MyHeaderName", "myHeaderValue");

            // Setting the Subject and Content Type
            msg.setSubject(emsg.subject, "UTF-8");
            // msg.setSubject(emsg.subject);//text/plain; charset=ISO-8859-1
            // msg.setContent( map.getValue().getContent(), "text/html");
        } catch (AddressException e) {

            e.printStackTrace();
        } catch (MessagingException e) {

            e.printStackTrace();
        }
        return msg;

2 个答案:

答案 0 :(得分:1)

两种可能性。

首先,您确定emsg.content包含非广域数据吗?如果它在您使用此代码之前已损坏,那么您已经不幸了。

其次,如果content没问题,请尝试使用MimeBodyPart.setText()

        if (emsg.isPlain) {
            messageContentPart.setText(content, "UTF-8", "plain");
        } else {
            messageContentPart.setText(content, "UTF-8", "html");
        }

答案 1 :(得分:0)

我认为虽然你设置的内容类型字符集UTF-8你不写UTF8流。你在使用BufferedWriter吗?检查是否将正确的charset传递给其构造函数。