使用JavaMail从GMail中读取完整的电子邮件 [英] Reading the full email from GMail using JavaMail
问题描述
我正在使用javamail,并且无法从我的gmail电子邮件中获取HTML。我有以下几点:
Session session = Session.getDefaultInstance(props,null);
Store store = session.getStore(imaps);
store.connect(imap.gmail.com,myemail@gmail.com,密码);
System.out.println(store);
文件夹收件箱= store.getFolder(收件箱);
inbox.open(Folder.READ_ONLY);
消息消息[] = inbox.getMessages(); (消息消息:消息)
{
System.out.println(消息); // com.sun.mail.imap.IMAPInputStream@cec0c5
以上所有工作都很好,但我可以打印或获取实际的HTML或文本电子邮件。我只是得到某种InputStream,我怎么轻松处理这个邮件来获取原始的HTML电子邮件?
我也尝试了循环消息, t让我非常感兴趣:
消息消息[] = inbox.getMessages();
for(int i = 0,n = message.length; i< n; i ++){
System.out.println(i +:+ message [i] .getFrom ()[0]
+\ t+ message [i] .getSubject());
String content = message [i] .getContent()。toString();
if(content.length()> 200)
content = content.substring(0,600);
System.out.print(content);
}
感谢任何hlep 。 您需要阅读整个流以阅读整个消息。例如,此SO帖子 a>详细说明如何将整个 InputStream
写入 OutputStream
,例如 System.out
使用Apache库。这将是一个开始的好地方,因为您可以将整个邮件正文打印到控制台。否则,您需要使用一些缓冲区等来将数据从流中提取出来,并将其放入任何您想要放入的位置。还有这篇SO帖子详细介绍了如何使用相同的库转换 InputStream
转换为字符串
。
I am making use of javamail and I am having trouble getting the HTML from my gmail emails. I have the following:
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "myemail@gmail.com", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
System.out.println(message); // com.sun.mail.imap.IMAPInputStream@cec0c5
The above all works fine but I can't print or get the actual HTML or Text email. I just get some sort of InputStream, how do I deal with this easily to get the raw HTML of the email?
I also tried looping through the message but that didn't get me very far:
Message message[] = inbox.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
String content = message[i].getContent().toString();
if (content.length() > 200)
content = content.substring(0, 600);
System.out.print(content);
}
Thanks all for any hlep.
The InputStream
object contains the body of the email. You need to read the entirety of the stream to read the entire body of the message. For instance, this SO post details how to write an entire InputStream
to an OutputStream
such as System.out
using an Apache library. That would be a good place to start as you could print the entire message body to the console. Otherwise, you'll need to use some buffers, etc, to pull the data out of the stream and put it into whatever you want to put it in. There is also this SO post that details, using the same library, how to convert an InputStream
into a String
.
这篇关于使用JavaMail从GMail中读取完整的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!