Regarding mail content reading

194 views
Skip to first unread message

Ketan Barapatre

unread,
Aug 3, 2011, 9:50:15 AM8/3/11
to Gmail4j
Hello All,
I am Ketan. I'm trying to read the mails from my Gmail
account but getting exception. I read the docs but I did not
understand
Why I'm getting this exception?
I want to know How can I read the message?

I would like to contribute to this project for this
functionality.

Thank You
Ketan Barapatre

Rajiv

unread,
Aug 4, 2011, 12:30:18 AM8/4/11
to Gmail4j
Hi ketanbarapatre,
what is the exception you are getting?

Ketan Barapatre

unread,
Aug 4, 2011, 2:11:40 AM8/4/11
to gma...@googlegroups.com
Thank You for reply.

I am pasting the code and the exception too.

Code

         GmailConnection conn = new ImapGmailConnection();
        char[] password = {'p', 'a', 's', 'w', 'o', 'r', 'd''};
        Credentials credentials = new Credentials("myusername", password.toCharArray());
        conn.setLoginCredentials(credentials);
        GmailClient client = new ImapGmailClient();
        client.setConnection(conn);
        List<GmailMessage> unreadMessages = client.getUnreadMessages();
        for (GmailMessage gmailMessage : unreadMessages) {
              System.out.println(""+gmailMessage.getContentText());
         }

Exception in thread "main" java.lang.UnsupportedOperationException: This GmailMessage implementation does not provide getContentText()
at com.googlecode.gmail4j.GmailMessage.getContentText(GmailMessage.java:197)
at test.IMAPTest.main(IMAPTest.java:43)
Java Result: 1

Please guide me how to read the message.

Thank You
Ketan barapatre

--
You received this message because you are subscribed to the Google Groups "Gmail4j" group.
To post to this group, send email to gma...@googlegroups.com.
To unsubscribe from this group, send email to gmail4j+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gmail4j?hl=en.




--
Please download an attachment

Regards
Ketan Barapatre


Rajiv Perera

unread,
Aug 4, 2011, 3:06:39 AM8/4/11
to gma...@googlegroups.com
Hi Ketan,
use the following method to retrieve unread messages:

   public void testGetUnreadMessages() {
        final ImapGmailClient client = new ImapGmailClient();
        final ImapGmailConnection connection = new ImapGmailConnection();

        try {
            connection.setLoginCredentials(new Credentials("<USERNAME>", "<PASSWORD>".toCharArray()));
            if (conf.useProxy()) {
                connection.setProxy(conf.getProxyHost(), conf.getProxyPort());
                connection.setProxyCredentials(conf.getProxyCredentials());
            }
            log.debug("Getting unread messages");
            client.setConnection(connection);
            final List<GmailMessage> messages = client.getUnreadMessages();
            for (GmailMessage message : messages) {
                log.debug(message);
            }
            assertNotNull("Messages are not null", messages);
        } catch (final Exception e) {
            log.error("Test Failed", e);
            fail("Caught exception: " + e.getMessage());
        } finally {
            if (connection.isConnected()) {
                connection.disconnect();
            }
        }
    }

let me know if this works out for you.

cheers,
Rajiv
Rajiv

Ketan Barapatre

unread,
Aug 9, 2011, 3:19:28 PM8/9/11
to gma...@googlegroups.com


Hello Rajiv Sir,

Sorry for late reply actually I was struggling
with some Maven settings.
I tried with the code you have provided above I check out the
source compile and run ImapGmailClientTest.java but still I was unable
to retrieve the text of the message.
According to observation I found
1 . javax.mail.internet.MimeMultipat type message are not being read
2 . Some simple message and HTML content message are read properly for
e.g.Mail sent by you was read properly

Please guide me If i have mistaken somewhere or How can i achieve reading ?

Regards
Ketan Barapatre

Ketan Barapatre

unread,
Aug 12, 2011, 1:55:10 PM8/12/11
to gma...@googlegroups.com
Hello All,

I am waiting for solution.
How to read message Content ?
Hope I will get some help this weekend.

Thank You

Ketan Barapatre

unread,
Aug 25, 2011, 7:50:36 AM8/25/11
to gma...@googlegroups.com
Hello All,

I have been waiting for mail content reading code.
If current stratus of API has any problem in reading contents.
Please let me know and Suggest me What to read to get it done . I will
develop that part to contribute in project.

Please let me know How can I contribute to this project?

Regards
Ketan Barapatre

Rodorush

unread,
Aug 15, 2012, 4:17:30 PM8/15/12
to gma...@googlegroups.com
Hello Ketan, I getting the same problem. Did you find some solution? Including reading MimeMultipart emails?

Ketan Barapatre

unread,
Aug 16, 2012, 1:12:48 AM8/16/12
to gma...@googlegroups.com
Hi

Please take look on code below. Let me know if it works for you.

private String geteDataFromMessage(org.apache.camel.Message message) {
Object body = message.getBody();
System.out.println(" body " + body);
if (body instanceof String) {
data = (String) body;
return data;
}
if (body instanceof MimeMultipart) {
try {
MimeMultipart content = (MimeMultipart) body;
for (int index = 0; index < content.getCount(); index++) {
BodyPart part = content.getBodyPart(index);
ContentType ct = new ContentType(part.getContentType());
//System.out.println("Content-type: " + ct.getPrimaryType() + "/" + ct.getSubType());
data = data + part.getContent();
//System.out.println("Content" + part.getContent());
}
} catch (IOException ex) {
Logger.getLogger(FilteredMessage.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
Logger.getLogger(FilteredMessage.class.getName()).log(Level.SEVERE, null, ex);
}
}
return data;
}
--
You received this message because you are subscribed to the Google Groups "Gmail4j" group.
To view this discussion on the web visit https://groups.google.com/d/msg/gmail4j/-/VyVkrW8-dAAJ.

To post to this group, send email to gma...@googlegroups.com.
To unsubscribe from this group, send email to gmail4j+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gmail4j?hl=en.



--

Regards
Ketan Barapatre


Rodolfo Andrade

unread,
Aug 19, 2012, 9:00:30 AM8/19/12
to gma...@googlegroups.com
Hello Ketan, thanks for reply, I´ll implement your code in my project and soon I return with the results.

Best regards,

Rodolfo Pereira de Andrade
Análise e Desenvolvimento de Sistemas - IFSP
Cel.: +55(16)9193-6808 - Skype: rodolfop.andrade



2012/8/16 Ketan Barapatre <ketanba...@gmail.com>

Ketan Barapatre

unread,
Aug 19, 2012, 10:04:44 AM8/19/12
to gma...@googlegroups.com

If it doesnt work.  Plz let me know. We can share idea and develop it.

Thank you
Ketan

Reply all
Reply to author
Forward
0 new messages