java gmail imap,使用JavaMail从GMail中读取完整的电子邮件

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 要使用JavaMail API和IMAP协议批量获取邮件正文,可以按照以下步骤进行: 1. 导入JavaMail API库。 2. 创建一个IMAP会话对象,连接到邮件服务器。 ```java Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); Session session = Session.getInstance(props, null); Store store = session.getStore(); store.connect("mail.server.com", "username", "password"); ``` 3. 打开INBOX文件夹,获取所有的邮件。 ```java Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); ``` 4. 遍历所有邮件,获取邮件正文。 ```java for (Message message : messages) { String subject = message.getSubject(); String from = message.getFrom()[0].toString(); String text = getTextFromMessage(message); System.out.println("Subject: " + subject); System.out.println("From: " + from); System.out.println("Text: " + text); } ``` 5. 获取邮件正文的方法。 ```java private String getTextFromMessage(Message message) throws MessagingException, IOException { String result = ""; if (message.isMimeType("text/plain")) { result = message.getContent().toString(); } else if (message.isMimeType("multipart/*")) { MimeMultipart mimeMultipart = (MimeMultipart) message.getContent(); int count = mimeMultipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = mimeMultipart.getBodyPart(i); if (bodyPart.isMimeType("text/plain")) { result = result + "\n" + bodyPart.getContent(); break; } } } return result; } ``` 以上就是使用JavaMail API和IMAP协议批量获取邮件正文的基本步骤。需要注意的是,邮件服务器的配置信息需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值