java JavaMail 检查邮件内容 gmail IMAP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12988799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaMail check message content gmail IMAP
提问by eatonphil
I am trying to read my messages, I can get it to print the header but the from and the content are displayed funny. Here is the code I am using to display the messages:
我正在尝试阅读我的消息,我可以让它打印标题,但来自和内容的显示很有趣。这是我用来显示消息的代码:
int j = message.length-1;
for (int i=j;i>=0;i--) {
System.out.println("Message " + (i + 1));
System.out.println("From : " + message[i].getFrom());
System.out.println("Subject : " + message[i].getSubject());
try {
System.out.println("Body: " + message[i].getContent());
} catch (IOException ex) {
System.out.println(ex);
}
}
The output is as follows:
输出如下:
Message 1:
From: [javax.mail.internet.InternetAddress;@175831e]
Subject: Hello //This is correct
Body: javax.mail.internet.MimeMultipart@15b5219
Why doesn't this print out the actual email address for the from statement? And why doesn't it print out the actual body content? (I am only interesting in the plain text.)
为什么这不打印 from 语句的实际电子邮件地址?为什么它不打印出实际的正文内容?(我只对纯文本感兴趣。)
Whole code:
全码:
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import java.util.*;
import com.sun.mail.imap.*;
import java.io.*;
public class MailClient {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("mail.store.protocol","imaps");
Session session;
session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com","[email protected]","password");
IMAPFolder folder = (IMAPFolder) store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen,false);
Message message[] = folder.search(unseenFlagTerm);
int j = message.length-1;
for (int i=j;i>=0;i--) {
System.out.println("Message " + (i + 1));
System.out.println("From : " + message[i].getFrom());
System.out.println("Subject : " + message[i].getSubject());
try {
System.out.println("Body: " + message[i].getContent());
} catch (IOException ex) {
System.out.println(ex);
}
}
System.out.println(newMsg);
folder.close(false);
store.close();
}
catch (MessagingException e) {
System.out.println("Error: " + e);
}
}
}
Thanks!
谢谢!
回答by ThePCWizard
For plain text and html messages:
对于纯文本和 html 消息:
String content= messages[i].getContent().toString();
String content= messages[i].getContent().toString();
For Multipart Messages:
对于多部分消息:
Multipart multipart = (Multipart) messages[i].getContent();
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) { // BodyPart.ATTACHMENT doesn't work for gmail
System.out.println("Mail have some attachment");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
}
else {
System.out.println("Body: "+bodyPart.getContent());
content= bodyPart.getContent().toString();
}
回答by Bill Shannon
The JavaMail FAQ tells you how to access the main text body of a message.
JavaMail FAQ 告诉您如何访问消息的主要文本正文。
回答by Satya
change this
改变这个
message[i].getFrom()
to
到
message[i].getFrom()[0].toString())
回答by Petro Gordiyevich
Try to use next:
尝试使用下一个:
message[i].writeTo(System.out);