0

I am trying to write a simple code that will read messages from gmail inbox. I have found some examples, but non of them is working. Most promising is code I've found on CompilatimEerror.com, BUT whatever I try I get this error:

java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart

Here is my code:

import java.util.*;
import javax.mail.*;

public class ReadingEmail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect("imap.gmail.com", "yourEmailId@gmail.com","password");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            Message msg = inbox.getMessage(inbox.getMessageCount());
            Address[] in = msg.getFrom();
            for (Address address : in) {
                System.out.println("FROM:" + address.toString());
            }
            Multipart mp = (Multipart) msg.getContent(); // here it breaks
            BodyPart bp = mp.getBodyPart(0);
            System.out.println("SENT DATE:" + msg.getSentDate());
            System.out.println("SUBJECT:" + msg.getSubject());
           System.out.println("CONTENT:" + bp.getContent());
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }
}

There is no connection error, as it gets the subject, date, and all this stuff, but the email body is a mystery. There will be no attachments, I will get only simple mails(this is a part of greater project)

What I am looking for is to read the unread mail (and then delete this message, so the inbox will be permanently empty(spam will be deleted manually)). I lack knowledge about web programing/structures, and all that pop's, imaps and stuff is a blank space. Also keep in mind that I am a novice programmer and this is the first time that I go outside of my computer with my code unfortunately straight into the problems of protocols / authentication / getting things from internet.

I went through a lot of pages, but never found an explanation that would allow me to create it myself...

Durden
  • 1
  • 2
  • What is the error you're getting. – CubeJockey May 04 '15 at 15:29
  • java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart – Durden May 04 '15 at 15:32
  • Sometimes you should just google the error / exception you're getting. Let us know if the following link helps you. In short: _"Your message content [is] returning String and you are trying to type cast to Multipart"_ http://stackoverflow.com/questions/23116465/java-lang-classcastexception-java-lang-string-cannot-be-cast-to-javax-mail-mult – CubeJockey May 04 '15 at 15:34
  • For me it just says, what the exception says. I cannot cast the body which is orginally a string to ?Multipart? I tried to cast body to (String) with no multipart mentioned in code, but that wasn't working. – Durden May 04 '15 at 15:42
  • I solved it finally. Sadly there is not so much help to look in this matter. – Durden May 04 '15 at 15:51
  • Please post your solution as an answer to this post. That will help other people who have the same problem. :) – CubeJockey May 04 '15 at 15:59

1 Answers1

0

Are you related to this guy?

The JavaMail FAQ has pointers to lots of helpful information, including the JavaMail project page, sample code, etc. You'll also find pointers to some useful background material and tutorials here.

Community
  • 1
  • 1
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40