problem in viewing the mail's content using javamail

problem in viewing the mail's content using javamail

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 }**

View Answers









Related Tutorials/Questions & Answers:
problem in viewing the mail's content using javamail
problem in viewing the mail's content using javamail  i'm using the following code to view a mail in a javamail application problem i'm facing... as if they are body of the mail. Secondly in some mails i'm getting the following Exception
send without authentication mail using java - JavaMail
send without authentication mail using java  Dear All, I am tring to send simple mail without authentication using java. is it possible, could...(Message.RecipientType.TO, "to mail id"); msg.setSubject("JavaMail hello world
Advertisements
extract content from javamail - JavaMail
extract content from javamail  Hi, I currently try the example(Read Multipart mail using Java Mail) on Javamail from the site to receive the e-mail. Then If I going to extract text the message content. What can I do
values of the "from address & to addresses" while sending a mail to localhost using javamail
values of the "from address & to addresses" while sending a mail to localhost using javamail  Hi, I verified javamail tutorial which is mentioned in JavaMail in ur website.I tried 1st program for sending the Mail.It's
Java Mail - JavaMail
Java Mail  how to email using Java mail or automatic generated mail???  Hi Friend, Please visit the following link: http://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks
How i can send mail by using jsp.............. - JavaMail
How i can send mail by using JSP  Hi, will you please tell me how i can send mail by using jsp. Tell me in detail. Thanks!  Example and JSP... to create a send mail program using JSP.JSP Send Mail Program Resources:-http
java mail api - JavaMail
java mail api  Hi, Pls give me the detailed information on how... in the code..   Hi friend, For solving the problem remember some points : 1.Download the Apache Mail server (James) http://james.apache.org
Version of javamail>mail dependency
List of Version of javamail>mail dependency
java mail error - JavaMail
java mail error  how to rceive emails using java mail api  Hi Friend, We can use Java Mail APIs to send and receive mails in Java.... Please visit the following link to know more about use of Java Mail API. http
JAVA MAIL - JavaMail
application using JAVA MAIL API.  Hi Friend, Please explain us what code you are using so that we can rectify the problem. If you are getting the Email...JAVA MAIL  Hi! I am doing project called "E-mail Client
problem in record viewing in jsp - JSP-Servlet
problem in record viewing in jsp  hai i send the code can you please find where is the problem it is not showing any error but i cannot able to view... and servlet and back end mysql actually what is my problem is i have to everytime
JAVA MAIl - JavaMail
JAVA MAIl  Hi, I am trying to send a mail using java mail api. But I am getting following error: javax.mail.SendFailedException: Sending failed...: http://www.roseindia.net/javamail/ Hope that it will be helpful for you
Problem in record viewing in jsp - JSP-Servlet
Problem in record viewing in jsp  hai i have developed the application using jsp,servlets in eclipse ide i have to insert,delete,update and view . no problem all are working but in view part alone some problem i dont know what
java mail - JavaMail
java mail  how to send a mail without authentication in java ?  Hi Friend, Please visit the following link: http://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks
sent mail code - JavaMail
sent mail code  hi i have done a project in my final year.acually... a folder like SENT MAILS.so i need a sugestte code to that.pls send to my mail... . This link contains examples over the Java Mail API.. http://www.roseindia.net
Java Mail exception - JavaMail
Java Mail exception  Hi! I am developing E-mail Client (GMAIL) program using JSP technology. Till now i dont have any problem. From last 3 days while i am connecting to the Gmail, I am getting the following exception... Whats
gmail access using javamail API
gmail access using javamail API  how to coonect to gmail using java mail API ?  Hi Friend, Please visit the following link: http://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks   
Sending mail - JavaMail
Sending mail  Need a simple example of sending mail in Java  Hi,To send email you need a local mail server such as apache james. You first download apache james mail server and install on the local network.Then create
viewing
viewing  viewing from session and display e-ticket using struts
problem getting password - JavaMail
problem getting password  hi i am trying to get password but igot following error Error sending mail:javax.mail.MessagingException: Could not connect to SMTP host: 192.168.10.14, port: 25;nested exception
problem in getting mail exchange records(MX) using DNS in java
problem in getting mail exchange records(MX) using DNS in java  Hi, jsp file: <%@page import="javax.naming.NamingEnumeration"%> <...) at java.lang.Thread.run(Thread.java:619) Note : I am using glassfish v3. above code works fine
problem in getting mail exchange records(MX) using DNS (glassfish(struts2))
problem in getting mail exchange records(MX) using DNS (glassfish(struts2))  Hi, dummy.jsp <%@page import="javax.naming.Context"%> <%@page import="java.util.Hashtable"%> <%@page import
Java Mail API - JavaMail
account using java mail api... i got code from our roseindia.net i.e....Java Mail API  Hi! I wish u advanced happy new year to all of ur... exception... Please solve my problem... Thanks in advance... Exception
JAVA Mail - JavaMail
JAVA Mail  Hi! I am trying to send the mail using SMTP of gmail. program was compiled successfully. when i am trying to execute that program it is showing error --- Must issue STARTTLS command first... How to avoid
mail problem
mail problem  Sir, I tried ur send mail example but it giveng...; <title>Mail API</title> </head> <body><table...;Mail API</h1> <tr>
email problem - JavaMail
email problem  hi,i want to write a code for sending a mail to a company employees having strength more than 1000.I had fetched the mail ids from database.I want to send mail to employee with mail id in 'TO' column.To column
java mail - JavaMail
java mail  HI! I am developing email client. Using IMAP i am displaying all the messages which are in "INBOX". I want to know how to access remining folders such as "sent", "draft" and others..... Thanks in advance
get from address using javamail api?
get from address using javamail api?  i want to get from address of particular mail using the javamail api? actually i had done it in one .jsp file using the particular mail number. message[messageno].getFrom()[0
Maven Repository/Dependency: javamail | mail
Maven Repository/Dependency of Group ID javamail and Artifact ID mail. Latest version of javamail:mail dependencies. # Version Release Date You can read more at: Maven Tutorials
Mail from JSP with SMTP - JavaMail
Mail from JSP with SMTP  Hi, Can any one pls guide me how to send mail from JSp page by using SMTP IP address. Regards...,Britto.M  Hi Friend, Please visit the following link: http://www.roseindia.net/mail
sending mail using jsp
sending mail using jsp  please give me the detailed procedure and code for sending mail through jsp program   Please visit the following...://www.roseindia.net/javamail
what is java mail API - JavaMail
what is java mail API  Please tell me in detail what is java mail API
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
i have problem in that program - JavaMail
i have problem in that program  1. Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number... a simple OPAC system for library using even-driven and concurrent programming
org.codehaus.plexus - plexus-mail-sender-javamail version 1.0-alpha-4 Maven dependency. How to use plexus-mail-sender-javamail version 1.0-alpha-4 in pom.xml?
org.codehaus.plexus  - Version 1.0-alpha-4 of plexus-mail-sender-javamail... 1.0-alpha-4 of plexus-mail-sender-javamail in pom.xml? How to use plexus-mail-sender... of plexus-mail-sender-javamail in project by the help of adding the dependency
Forwarding Messages using Java Mail
using javamail api. there is no method to forward a mail from one user to another... Forwarding Messages using Java Mail   ... and get message content then send its for another user. Java code given below
send the mail with attachment problem - Struts
send the mail with attachment problem  Hi friends, i am using... mail server properties.setProperty("mail.smtp.host", host... InternetAddress(to)); message.setSubject("JavaMail Attachment
iphone mail sending problem
iphone mail sending problem  Hi, I'm receiving the following error ... while sending mail in my iphone application Terminating app due to uncaught... it done myself... actually in my send mail code i have predefined the mailto id
Maven Repository/Dependency: org.codehaus.plexus | plexus-mail-sender-javamail
Maven Repository/Dependency of Group ID org.codehaus.plexus and Artifact ID plexus-mail-sender-javamail. Latest version of org.codehaus.plexus:plexus-mail-sender-javamail dependencies. # Version Release Date
i have problem in that program sir - JavaMail
i have problem in that program sir  1.Develop multi-threaded echo server and a corresponding GUI client in Java
java mail send using setText()
); Here am using set Text() method to send mail content in html format. if i use set Text() method mail content is display same structure what i wrote the code...java mail send using setText()  Hai , Am newly mail send portion
How to read yahoo mail using java mail api - WebSevices
How to read yahoo mail using java mail api  Hi there .... i wanted to know how to read mail from yahoo using pure java code. Is there any one who can...://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks
send mail using JavaScript
send mail using JavaScript  How can we send mail using JavaScript?   Hi friends, You can not send email directly using JavaScript. But you can use JavaScript to execute a client side email program send the email using
send mail using smtp in java
send mail using smtp in java  How to send mail using smtp in java?   Sending mail in JSP - SMTP
Read attachment message using Java Mail
a Attachment message using javamail api. Java Mail API provides classes to send multiple... Read attachment message using Java Mail   ...;2008 eml mail Return-Path: <test@localhost> Delivered
Sending message using Java Mail
Sending message using Java Mail   ... using javamail api. A client create new message by using Message subclass. It sets attributes like recipient address and the subject, and inserts the content
Read Multipart mail using Java Mail
message using javamail api. When we read a multipart mail firstly create... Read Multipart mail using Java Mail   ... bodyPart content using getContent() method.ADS_TO_REPLACE_1

Ads