
i'm using the following code to view a mail in a javamail application problem i'm facing is if there are any attachments of .txt format they are also displayed as if they are body of the mail. Secondly in some mails i'm getting the following Exception :
java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
also pls guide me how to extract the body and attachments of the mail
Code which i've used is :
**package desktopapplication1; import javax.mail.*; import java.io.*; import java.util.Properties.*; import javax.swing.*; import javax.swing.table.*; import javax.mail.internet.*; import java.util.*; import desktopapplication1.Fetchmail; import javax.mail.Multipart.*; import desktopapplication1.Start.*; import javax.activation.DataHandler; /** * * @author Karan */ public class Viewmail { String subject; List<String> list=new ArrayList<String>(); String st=null; /*Viewmail(String name) { this.subject=name; showmail(subject); }*/ public List showmail(String name) { String user=Start.getUsername(st); Properties props = System.getProperties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.transport.protocol", "smtp"); Session mailSession = Session.getDefaultInstance(props,null); File file=new File("mails/"+user+"/"+Fetchmail.replacename(name)+".eml"); System.out.println(file.getPath()); String files; try{ files=file.getPath(); File emlFile = new File(files); InputStream source = new FileInputStream(emlFile); MimeMessage message = new MimeMessage(mailSession, source); Date date = message.getSentDate();
String ssfrom = InternetAddress.toString(message.getFrom());
String subj = message.getSubject();
String sdate=String.valueOf(date);
StringBuilder build=new StringBuilder();
String body=new String();
String body2=new String();
InputStream stream = message.getInputStream();
while(stream.available()!= 0) {
build.append((char)stream.read());
}
if (message.getContentType().equals("text/html")||message.getContentType().equals("text/plain"))
{ body2=(String)message.getContent();
}
body=build.toString();
list.add(ssfrom);
list.add(subj);
list.add(sdate);
//list.add(body);
Object object=message.getContent();
Multipart mp=(Multipart)object;
Part[] pa=new Part[mp.getCount()];
Part p=null;
for(int j=0;j<mp.getCount();j++)
{ p=mp.getBodyPart(j);
p.getContentType();
String disposition=p.getDisposition();
if(p.getContentType().startsWith("text/plain") ||
p.getContentType().startsWith("text/html"))
{ body2=(String)p.getContent();
}
if(disposition!=null&&(disposition.equals(BodyPart.ATTACHMENT)))
{ System.out.println("there is an attachment");
DataHandler handler=p.getDataHandler();
System.out.println("file name: "+handler.getName());
}
}
list.add(body2);
}//end of try
catch (Exception e)
{System.out.println(e);
}//end of catch
return list; }//end of show }**
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.