我正在使用JavaMail API在我的Android手机的电子邮件客户端上工作。如果我尝试获取电子邮件的内容,我会在logCat中获得以下文本:
FROM:XXXXXXXXXXXXXXXXXXXXXXXXXX
TO:AG Blasorchester
主题:BITTE DENKT和DIE SCHWARZE MAPPE / RINGORDNER
SendDate:2月27日星期三21:30:49 MEZ 2013
CONTENT-TYPE:TEXT / PLAIN;字符集= ISO-8859-1
讯息内容:
Fehler:java.lang.ClassCastException: com.sun.mail.util.QPDecoderStream无法强制转换为java.lang.String
这是抛出的异常:java.lang.ClassCastException:com.sun.mail.util.QPDecoderStream无法强制转换为java.lang.String
我使用以下代码获取内容:
public static void dumpPart(Part p) throws Exception {
Log.i("Gestartet", "dumpPart Gestartet");
if (p instanceof Message)
dumpEnvelope((Message)p);
/** Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(is);
int c;
while ((c = is.read()) != -1)
System.out.write(c);
**/
String ct = p.getContentType();
try {
pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
} catch (ParseException pex) {
pr("BAD CONTENT-TYPE: " + ct);
}
String filename = p.getFileName();
if (filename != null)
pr("FILENAME: " + filename);
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
try{
if (p.isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
if (!showStructure && !saveAttachments)
System.out.println((String)p.getContent());
} else if (p.isMimeType("multipart/*")) {
pr("This is a Multipart");
pr("---------------------------");
Multipart mp = (Multipart)p.getContent();
level++;
int count = mp.getCount();
for (int i = 0; i < count; i++)
dumpPart(mp.getBodyPart(i));
level--;
} else if (p.isMimeType("message/rfc822")) {
pr("This is a Nested Message");
pr("---------------------------");
level++;
dumpPart((Part)p.getContent());
level--;
} else {
if (!showStructure && !saveAttachments) {
/*
* If we actually want to see the data, and it's not a
* MIME type we know, fetch it and check its Java type.
*/
Object o = p.getContent();
if (o instanceof String) {
pr("This is a string");
pr("---------------------------");
System.out.println((String)o);
} else if (o instanceof InputStream) {
pr("This is just an input stream");
pr("---------------------------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
} else {
pr("This is an unknown type");
pr("---------------------------");
pr(o.toString());
}
} else {
// just a separator
pr("---------------------------");
}
}
}catch(Exception e){
Log.i("Fehler", "Fehler: " + e);
}
}
我可以将消息内容作为字符串获取而没有例外吗?
答案 0 :(得分:0)
您的示例中的From:XXXX部分或标题中的其他位置是否有任何德语特殊字符?该电子邮件是RFC 2047编码的,当电子邮件标题包含non-US ASCII个字符时会发生这种情况。
此代码可能有效。完全未经测试,因为我没有设置环境,但可能是一个起点。
if (p.isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
if (!showStructure && !saveAttachments) {
if (p.getContent instanceof String) {
System.out.println((String)p.getContent());
} else if (p.getContent() instanceof QPDecoderStream) {
BufferedInputStream bis = new BufferedInputStream(p.getContent());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
int c = bis.read();
if (c == -1) {
break;
}
baos.write(c);
}
System.out.println(new String(baos.toByteArray()));
} else {
//Handle different content types
}
}
}