简体   繁体   中英

Reading the full email from GMail using JavaMail

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 issue is that the data you get is typically the raw data for a mime/multipart stream. You need to do something like this:

for(Message message:messages) {
  if(javax.mail.Multipart.class.isInstance(message)){
    Multipart parts = (Multipart)msg.getContent(), innerPart;
    int i;
    for(i=0;i<parts.getCount();i++){
      javax.mail.BodyPart p = parts.getBodyPart(i);
      if("text/html".equals(p.getContentType())){
        // now you can read out the contents from p.getContent()
        // (which is typically an InputStream, but depending on your javamail
        // libraries may be something else
      }
    }
  }
}

Good luck.

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 .

You could use IOUtils of Apache Commons or can possibly even try something along the lines of :

BufferedReader br = new BufferedReader(new InputStreamReader(daInputStream));
String oneLine = "";
while ( (oneLine = br.readLine()) !=  null )
    System.out.println(oneLine);

如果你使用java邮件,你可以使用“multipart”和“bodypart”对象来浏览电子邮件消息,以提取“text / plain”和“text / html”内容,这些内容就是你想要的内容。

your could try with the MimeMessage class:

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "myemail@gmail.com", "password");

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
    MimeMessage im = new MimeMessage(session, message.getContent());
    im.getFrom();
    im.getMessageID();
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM