[英]JavaMail - Parsing email content, can't seem to get it to work! (Message.getContent())
几个星期以来,我一直在为android开发一个电子邮件客户端,我一直忽略解析电子邮件内容一段时间,因为我从来没有能够让它工作。 因此,现在是时候寻求帮助了!
我一直在环顾四周,我遇到过一些我尝试过的方法,但从来没有取得多大成功! 目前我最接近的尝试必须是:
private String parseContent(Message m) throws Exception
{
//Multipart mp = (Multipart)c;
//int j = mp.getCount();
/*for (int i = 0; i < mp.getCount(); i++)
{
Part part = mp.getBodyPart(i);
System.out.println(((MimeMessage)m).getContent());
content = content + part.toString();
//System.out.println((String)part.getContent());
}*/
Object content = m.getContent();
String contentReturn = null;
if (content instanceof String)
{
contentReturn = (String) content;
}
else if (content instanceof Multipart)
{
Multipart multipart = (Multipart) content;
BodyPart part = multipart.getBodyPart(0);
part.toString();
contentReturn = part.getContent().toString();
}
return contentReturn;
}
但它不起作用,我得到像“javax.mail.internet.MimeMultipart@44f12450”这样的胡言乱语。
任何人都可以看到我错在哪里?
谢谢,里斯
以上建议均无效。 你不需要在这里做任何复杂的事情。 Mimemessage有message.writeTo(outputStream);
您打印邮件所需的只是:
message.writeTo(System.out);
上面的代码会将实际的mime消息打印到控制台(或者你可以使用任何记录器)。
将内容保存到.eml ,您可以在Outlook中打开它。 就那么简单!
Multipart multipart = (Multipart) content;
BodyPart part = multipart.getBodyPart(0);
part.toString();
contentReturn = part.getContent().toString();
当你有BodyPart部分时,你应该继续测试
if(part.getContent()instanceof String){...}
我也得到了同样的错误,几乎每件事都尝试过,但解决方案对我有用
private String getFinalContent(Part p) throws MessagingException,
IOException {
String finalContents = "";
if (p.getContent() instanceof String) {
finalContents = (String) p.getContent();
} else {
Multipart mp = (Multipart) p.getContent();
if (mp.getCount() > 0) {
Part bp = mp.getBodyPart(0);
try {
finalContents = dumpPart(bp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return finalContents.trim();
}
private String dumpPart(Part p) throws Exception {
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
return getStringFromInputStream(is);
}
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
希望这会对某人有所帮助。
解析javax邮件时遇到了同样的问题。 在我的解决方法中,我发现了一个奇怪的事情。 从POP3列出邮件并没有给我邮件正文内容。 所以使用IMAP对我有用。 现在我能够解析Text / plain以及Text / Html并阅读正文。 要解析相同的我使用以下方法。
public String printMessage(Message message) {
String myMail = "";
try {
// Get the header information
String from = ((InternetAddress) message.getFrom()[0])
.getPersonal();
if (from == null)
from = ((InternetAddress) message.getFrom()[0]).getAddress();
System.out.println("FROM: " + from);
String subject = message.getSubject();
System.out.println("SUBJECT: " + subject);
// -- Get the message part (i.e. the message itself) --
Part messagePart = message;
Object content = messagePart.getContent();
// -- or its first body part if it is a multipart message --
if (content instanceof Multipart) {
messagePart = ((Multipart) content).getBodyPart(0);
System.out.println("[ Multipart Message ]");
}
// -- Get the content type --
String contentType = messagePart.getContentType();
// -- If the content is plain text, we can print it --
System.out.println("CONTENT:" + contentType);
if (contentType.startsWith("TEXT/PLAIN")
|| contentType.startsWith("TEXT/HTML")) {
InputStream is = messagePart.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String thisLine = reader.readLine();
while (thisLine != null) {
System.out.println(thisLine);
myMail = myMail + thisLine;
thisLine = reader.readLine();
}
}
System.out.println("-----------------------------");
} catch (Exception ex) {
ex.printStackTrace();
}
return myMail;
}
希望它可以帮助某人。
不完全确定,但听起来你想将MimeMessage转换为String。 严格来说,MimeMessage to String没有“标准”翻译,但是这是我几年前写的一段代码,试图生成一个这样的翻译。 只测试了英文消息,所以i18n必须是你自己想到的东西。
不幸的是,SO的愚蠢过滤器似乎认为我的代码示例是一个图像,不会让我发布它:看看http://pastebin.com/pi9u5Egq上的类:代码正在做一堆其他不必要的事情(它将它吐出到使用飞碟渲染的HTML中),还需要Jakarta Commons Lang,javamail和激活库,但它适用于我。 你会像这样调用它:
fetchText(message, null, false, false);
获取文本字符串。 尝试大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.