2017-06-08 69 views
1

我想使用javax.mail API發送的電子郵件中的文本發送笑臉。使用javax.mail發送電子郵件中的笑臉/表情

當我在unescaping之後sysout unicode時,它在控制檯上給出了正確的笑臉,但在郵件中顯示「?」 (問號)。

以下是我正在試圖做到這一點:
Unicode字符串:\ u0048 \ u0069 \ u0020 \ uD83D \ uDE0A \ uD83D \ uDE00 \ uD83D \ uDE01 \ uD83D \ uDE02

String uniMessage = "\u0048\u0069\u0020\uD83D\uDE0A\uD83D\uDE00\uD83D\uDE01\uD83D\uDE02"; 
String message = StringEscapeUtils.unescapeJava(uniMessage); 

SYSOUT在控制檯上輸出正確的信息爲「Hi」,而在電子郵件中顯示爲「Hi ????」。

Properties props = new Properties();

 props.put("mail.smtp.auth", "true"); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", SMTPHost); 
     props.put("mail.smtp.port", SMTPPort); 
     props.put("mail.smtp.user", username); 
     props.put("mail.smtp.password", password); 
     props.put("mail.smtp.socketFactory.port", SMTPPort); 
     props.put("mail.smtp.socketFactory.fallback", "true"); 

     Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, 
           password); 
        } 
       }); 
     session.setDebug(debug); 
     Message msg = new MimeMessage(session); 
     InternetAddress addressFrom = new InternetAddress(username); 
     msg.setFrom(addressFrom); 
     InternetAddress addressTo = new InternetAddress(to); 
     addressTo = new InternetAddress(to); 
     msg.setRecipient(Message.RecipientType.TO, addressTo); 
     msg.addHeader("Demo", "Demo"); 
     msg.setSubject(subject); 
     msg.setContent(message, "text/html"); 
     Transport.send(msg); 

以上是我的郵件服務。如何在電子郵件中正確發送笑臉/表情符號? 請幫忙。

+1

內容類型也應該包括一個字符編碼。 – RealSkeptic

+0

你的意思是它應該是msg.setContent(message,「text/html; UTF-8」); ? – User14141111

回答

1

有人做過由@RealSkeptic的建議如下:

msg.setContent(StringEscapeUtils.unescapeJava(message), "text/html; charset=UTF-8"); 
相關問題