For a few weeks I have been developing a email client for android, I have been ignoring parsing email content for a while as I have never been able to get it to work. Thus, the time has come to ask for help!
几个星期以来,我一直在开发一个android的电子邮件客户端,我一直忽略了对邮件内容的解析,因为我从来没有能够让它工作。因此,现在是寻求帮助的时候了!
I have been looking around and I have come across a few methods I have tried but never had much success with! Currently my closest attempt would have to be:
我一直在四处寻找,我发现了一些我尝试过但从未成功的方法!目前我最接近的尝试是:
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;
}
But it does not work and I get gibberish such as "javax.mail.internet.MimeMultipart@44f12450".
但它不工作,我就会胡扯,比如“javax.mail.internet.MimeMultipart@44f12450”。
Can anyone see where I am going wrong?
有人能看出我哪里出错了吗?
Thanks, Rhys
谢谢,里斯
5 个解决方案
#1
9
None of the above suggestions is valid. You don't need to do anything complex here. Mimemessage has got message.writeTo(outputStream);
以上建议均无效。你不需要在这里做任何复杂的事情。将有message.writeTo(outputStream);
All you need to print the message is:
你需要打印的信息是:
message.writeTo(System.out);
The above code will print the actual mime message to the console (or you can use any logger).
上面的代码将把实际的mime消息打印到控制台(或者您可以使用任何日志记录器)。
Save the content to .eml and you can open it in outlook. Simple as that!
将内容保存到.eml,您可以在outlook中打开它。就这么简单!
#2
4
Multipart multipart = (Multipart) content;
BodyPart part = multipart.getBodyPart(0);
part.toString();
contentReturn = part.getContent().toString();
When you have BodyPart part, you should keep testing
当你有身体的部分,你应该继续测试。
if(part.getContent() instanceof String){ ... }
如果(part.getContent()运算符字符串){…}
#3
1
I also got the same error any tried almost every thing, but the solution worked for me is
我也犯了同样的错误,几乎每件事都尝试过,但解决方法对我来说是有效的。
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();
}
Hope this will help someone.
希望这能帮助到别人。
#4
1
I had the same issues while parsing Message of javax mail. In my workaround i found a weird thing. Listing mail from POP3 was not giving me Mail body content. So used IMAP which worked for me. Now i'm able to parse Text/plain as well as Text/Html and read the body. To parse the same i used following method.
我在解析javax邮件的消息时遇到了相同的问题。在我的工作中,我发现了一件奇怪的事情。来自POP3的清单邮件没有给我邮件正文内容。所以我用了IMAP。现在,我可以解析文本/普通文本和文本/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;
}
Hope it helps someone.
希望它能帮助一些人。
#5
0
Not entirely sure, but what it sounds like is you want to convert a MimeMessage into a String. Strictly speaking, there is no "standard" translation for MimeMessage to String, but here is a chunk of code that I wrote a couple of years back that attempts to generate one such translation. Only tested on English messages, so i18n will have to be something you think about yourself.
不完全确定,但这听起来像是你想把一个MimeMessage转换成一个字符串。严格地说,对于MimeMessage来说,没有“标准”的翻译,但这里有一段代码,是我几年前写的,试图生成一个这样的翻译。只在英语信息上测试,所以i18n必须是你自己想的东西。
Unfortunately SO's stupid filter seems to think that my code sample is an image and won't let me post it: take a look at the class at http://pastebin.com/pi9u5Egq : the code is doing a bunch of otherwise unnecessary things (it was spitting it out into HTML that was rendered using flying saucer), and also requires Jakarta Commons Lang, javamail and activation libraries, but it works for me. You would invoke it like this:
不幸的是那么愚蠢的过滤器似乎认为我的代码示例是一个图像和不让我文章:http://pastebin.com/pi9u5Egq看看类:代码做了一堆不必要的事情(这是吐出来呈现为HTML,使用飞碟),同时也需要Jakarta Commons Lang、javamail和激活库,但它为我工作。你会这样调用它:
fetchText(message, null, false, false);
To get a text string. Try that out for size.
获取一个文本字符串。试试这个尺寸。
#1
9
None of the above suggestions is valid. You don't need to do anything complex here. Mimemessage has got message.writeTo(outputStream);
以上建议均无效。你不需要在这里做任何复杂的事情。将有message.writeTo(outputStream);
All you need to print the message is:
你需要打印的信息是:
message.writeTo(System.out);
The above code will print the actual mime message to the console (or you can use any logger).
上面的代码将把实际的mime消息打印到控制台(或者您可以使用任何日志记录器)。
Save the content to .eml and you can open it in outlook. Simple as that!
将内容保存到.eml,您可以在outlook中打开它。就这么简单!
#2
4
Multipart multipart = (Multipart) content;
BodyPart part = multipart.getBodyPart(0);
part.toString();
contentReturn = part.getContent().toString();
When you have BodyPart part, you should keep testing
当你有身体的部分,你应该继续测试。
if(part.getContent() instanceof String){ ... }
如果(part.getContent()运算符字符串){…}
#3
1
I also got the same error any tried almost every thing, but the solution worked for me is
我也犯了同样的错误,几乎每件事都尝试过,但解决方法对我来说是有效的。
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();
}
Hope this will help someone.
希望这能帮助到别人。
#4
1
I had the same issues while parsing Message of javax mail. In my workaround i found a weird thing. Listing mail from POP3 was not giving me Mail body content. So used IMAP which worked for me. Now i'm able to parse Text/plain as well as Text/Html and read the body. To parse the same i used following method.
我在解析javax邮件的消息时遇到了相同的问题。在我的工作中,我发现了一件奇怪的事情。来自POP3的清单邮件没有给我邮件正文内容。所以我用了IMAP。现在,我可以解析文本/普通文本和文本/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;
}
Hope it helps someone.
希望它能帮助一些人。
#5
0
Not entirely sure, but what it sounds like is you want to convert a MimeMessage into a String. Strictly speaking, there is no "standard" translation for MimeMessage to String, but here is a chunk of code that I wrote a couple of years back that attempts to generate one such translation. Only tested on English messages, so i18n will have to be something you think about yourself.
不完全确定,但这听起来像是你想把一个MimeMessage转换成一个字符串。严格地说,对于MimeMessage来说,没有“标准”的翻译,但这里有一段代码,是我几年前写的,试图生成一个这样的翻译。只在英语信息上测试,所以i18n必须是你自己想的东西。
Unfortunately SO's stupid filter seems to think that my code sample is an image and won't let me post it: take a look at the class at http://pastebin.com/pi9u5Egq : the code is doing a bunch of otherwise unnecessary things (it was spitting it out into HTML that was rendered using flying saucer), and also requires Jakarta Commons Lang, javamail and activation libraries, but it works for me. You would invoke it like this:
不幸的是那么愚蠢的过滤器似乎认为我的代码示例是一个图像和不让我文章:http://pastebin.com/pi9u5Egq看看类:代码做了一堆不必要的事情(这是吐出来呈现为HTML,使用飞碟),同时也需要Jakarta Commons Lang、javamail和激活库,但它为我工作。你会这样调用它:
fetchText(message, null, false, false);
To get a text string. Try that out for size.
获取一个文本字符串。试试这个尺寸。