Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail check message content gmail IMAP

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.)

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!

like image 328
eatonphil Avatar asked Oct 20 '12 13:10

eatonphil


People also ask

What is IMAP in Java?

IMAP is Acronym for Internet Message Access Protocol. It is an Application Layer Internet protocol that allows an e-mail client to access e-mail on a remote mail server. An IMAP server typically listens on well-known port 143. IMAP over SSL (IMAPS) is assigned to port number 993.

What is MimeMessage in Java?

MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message. The mail. mime. address. strict session property controls the parsing of address headers.


1 Answers

For plain text and html messages:

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();
            }
like image 123
ThePCWizard Avatar answered Nov 04 '22 14:11

ThePCWizard