2011-01-13 59 views
1

大家好,英镑符号不使用java.mail包在邮件内容中工作?

我使用javax.mail packaage MINEMESSAGE,MimeMultipart的类来发送邮件,但即使我提到型UTF-8,Unicode字符不是在正文的工作。像英镑符号不起作用。请帮忙做些什么。这里是我的邮件头

To: [email protected] 

Message-ID: <[email protected]> 

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; 
+0

是什么在邮件正文中? “£”是否应该显示为“= C2 = A3”? – dan04 2011-01-13 06:51:03

+1

那些编码的八位字节是“U-FFFD替换字符”的UTF-8表示。请显示您的实际代码,它有问题。 – 2011-01-13 08:38:58

回答

1

两种可能性。

首先,你确定emsg.content包含非静音数据吗?如果在获得此代码之前它已经损坏,那么您已经不幸运了。

其次,如果content是好的,请尝试使用MimeBodyPart.setText()代替:

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

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

相关问题