Java 邮件投掷 java.io.UnsupportedEncodingException: us-ascii big5
Java mail throwing java.io.UnsupportedEncodingException: us-ascii big5
我正在使用 java 邮件,遇到以下错误:java.io.UnsupportedEncodingException: us-ascii big5 at sun.nio.cs.StreamDecoder.forInputStreamReader
以下是导致此问题的 Mime header。
Content-Type: text/plain; charset="us-ascii, big5"
(我在内容上看到非英文字符)
这有效吗?有什么解决方案?
另一个相关问题,我看到导致此异常的字符集的不同变体(字符集值周围的 spl 字符):例如。
charset="'UTF-8'"
charset=`UTF-8`
charset=UTF=8
charset=utf
charset=\"UTF-8\" etc.,
请注意,这不仅发生在 utf-8 上,也发生在其他字符集上,但是电子邮件客户端(如 outlook 等)可以巧妙地打开和解码这些电子邮件。
有什么想法吗?
所有这些都是无效的字符集。只要有可能,请将此类问题报告给创建这些消息的程序的所有者。如果邮件是垃圾邮件(通常是垃圾邮件),请将其丢弃;这些错误是检测垃圾邮件的很好的启发式算法。
JavaMail FAQ 有处理这些错误的策略。
Can you try message.setHeader("Content-Type", "text/plain; charset=UTF-8")?
No, messages come in (i have no control) and i had to run javamail lib to parse to get content. the incoming messages are not created by me
Use the mail.mime.contenttypehandler
system property 转换内容类型而不实际修改电子邮件。
package cool.part.team;
import java.util.Arrays;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimePart;
public class EverythingIsAscii {
/**
* -Dmail.mime.contenttypehandler=cool.part.team.EverythingIsAscii
*/
public static void main(String[] args) throws Exception {
MimeMessage msg = new MimeMessage((Session) null);
msg.setText("test", "us-ascii, big5");
msg.saveChanges();
System.out.println("Transformed = "+ msg.getContentType());
System.out.println("Original = " + Arrays.toString(msg.getHeader("Content-Type")));
}
public static String cleanContentType(MimePart p, String mimeType) {
if (mimeType != null) {
String newContentType = mimeType;
try {
ContentType ct = new ContentType(mimeType);
String cs = ct.getParameter("charset");
if (cs == null || cs.contains("'")
|| cs.contains(",")) { //<--Insert logic here
ct.setParameter("charset", "us-ascii");
newContentType = ct.toString();
}
} catch (Exception ignore) {
//Insert logic to manually repair.
//newContentType = ....
}
return newContentType;
}
return mimeType;
}
}
将输出:
Transformed = text/plain; charset=us-ascii
Original = [text/plain; charset="us-ascii, big5"]
您必须更正此示例代码以正确转换字符集,因为一切都不是 ASCII。
我正在使用 java 邮件,遇到以下错误:java.io.UnsupportedEncodingException: us-ascii big5 at sun.nio.cs.StreamDecoder.forInputStreamReader
以下是导致此问题的 Mime header。
Content-Type: text/plain; charset="us-ascii, big5"
(我在内容上看到非英文字符)
这有效吗?有什么解决方案?
另一个相关问题,我看到导致此异常的字符集的不同变体(字符集值周围的 spl 字符):例如。
charset="'UTF-8'"
charset=`UTF-8`
charset=UTF=8
charset=utf
charset=\"UTF-8\" etc.,
请注意,这不仅发生在 utf-8 上,也发生在其他字符集上,但是电子邮件客户端(如 outlook 等)可以巧妙地打开和解码这些电子邮件。
有什么想法吗?
所有这些都是无效的字符集。只要有可能,请将此类问题报告给创建这些消息的程序的所有者。如果邮件是垃圾邮件(通常是垃圾邮件),请将其丢弃;这些错误是检测垃圾邮件的很好的启发式算法。
JavaMail FAQ 有处理这些错误的策略。
Can you try message.setHeader("Content-Type", "text/plain; charset=UTF-8")?
No, messages come in (i have no control) and i had to run javamail lib to parse to get content. the incoming messages are not created by me
Use the mail.mime.contenttypehandler
system property 转换内容类型而不实际修改电子邮件。
package cool.part.team;
import java.util.Arrays;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimePart;
public class EverythingIsAscii {
/**
* -Dmail.mime.contenttypehandler=cool.part.team.EverythingIsAscii
*/
public static void main(String[] args) throws Exception {
MimeMessage msg = new MimeMessage((Session) null);
msg.setText("test", "us-ascii, big5");
msg.saveChanges();
System.out.println("Transformed = "+ msg.getContentType());
System.out.println("Original = " + Arrays.toString(msg.getHeader("Content-Type")));
}
public static String cleanContentType(MimePart p, String mimeType) {
if (mimeType != null) {
String newContentType = mimeType;
try {
ContentType ct = new ContentType(mimeType);
String cs = ct.getParameter("charset");
if (cs == null || cs.contains("'")
|| cs.contains(",")) { //<--Insert logic here
ct.setParameter("charset", "us-ascii");
newContentType = ct.toString();
}
} catch (Exception ignore) {
//Insert logic to manually repair.
//newContentType = ....
}
return newContentType;
}
return mimeType;
}
}
将输出:
Transformed = text/plain; charset=us-ascii
Original = [text/plain; charset="us-ascii, big5"]
您必须更正此示例代码以正确转换字符集,因为一切都不是 ASCII。