0

When i am trying to retrieve gmail content through getcontent() method i am getting following exception

java.lang.ClassCastException: com.sun.mail.imap.IMAPInpustream cannot be cast to javax.mail.Multipart

So I converted the inputsream to string but it ios reurning all the data including which is not text/html. But i want only text/html data with attachment if any.

My code:

if(contentType.contains("multipart/alternative") || contentType.contains("multipart/MIXED")){
            Log.i("content type...","inside if stmt "+contentType);
            Multipart mp = (Multipart)gmsg.getGmailContent();
            try {
                for(int k=0;k<mp.getCount();k++){
                      javax.mail.BodyPart p = mp.getBodyPart(k);
                      if("text/html".equals(p.getContentType())){
                         /* gContent.loadDataWithBaseURL("https://mail.google.com",
                                  p.getContent().toString() , "text/plain","UTF-8", null); */
                          Log.i("msg", p.getContent().toString());
                      }
                    }
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                Log.i("error", e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

I have included mail.jar,activation.jar,additional.jar in libs folder.

Searched in google a lot but didn't get any solution. Plz help..Thanks in advance..

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
Madhu
  • 298
  • 1
  • 12

2 Answers2

0

If you want to read strings then:

Replace

if("text/html".equals(p.getContentType())){

with

if("text/plain".equals(p.getContentType())){
Mandar Limaye
  • 1,900
  • 16
  • 24
  • I am getting exception at line Multipart mp = (Multipart)gmsg.getGmailContent(); – Madhu Jul 13 '13 at 13:20
  • gmsg is a class in which i am storing different elements like subject of the mail,from,receipients and content as an object. – Madhu Jul 13 '13 at 14:06
  • So are you getting the error inside getGmailContent()? If its your own class why don't you return javax.mail.Multipart which is what you are trying to cast it as? – Mandar Limaye Jul 13 '13 at 14:20
0

You really should use the isMimeType method. Did you find the msgshow.java demo program?

The getContent method may not be able to convert the data to the correct Java class if the JavaBeans Activation Framework (JAF) configuration isn't working properly. JAF looks for META-INF/mailcap files to figure out how to convert MIME types to Java classes. There's such a configuration file in the JavaMail jar file. If the jar files are messed up, the configuration files are missing, or the class loader isn't working right to allow you to load these configuration files, the conversion won't work.

Also, the workaround here might help.

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