problem in viewing the mail's content using javamail

Ads
 

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

Ads









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
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
Advertisements
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
Version of javamail>mail dependency
List of Version of javamail>mail dependency
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
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
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
viewing
viewing  viewing from session and display e-ticket using struts
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
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>
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
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
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
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
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
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 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. ReadMultipartMail.java
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   ...;mail Return-Path: <[email protected]> Delivered-To: <
problem in using getKeyCode()
problem in using getKeyCode()  i am working with the jlist...while i was trying to use the arrow keys in the keyboard to select the values in the list it is not working properly...the code which i used is given below
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
i have problem in that program to my assignment sir - JavaMail
i have problem in that program to my assignment sir   Develop a programmer's editor in Java that supports syntax-highlighting, compilation support, debugging support, etc
javamail
javamail  i need java mail code,i had use some code,that give me ssl error in java,plz help me,i need urgent basises
Reading message using Java Mail
Reading message using Java Mail       This Example shows you how to read message using javamail.... store.connect(host, user, password) Connect to the current host using
Handle EML(Electronic mail) file with Java Mail
eml(Electronic mail) file using javamail api. eml is a simple email, all eml... Handle EML(Electronic mail) file with Java Mail...-Version: 1.0 Content-Type: text/plain; charset=us
Maven Dependency mail >> 1.3.1
You should include the dependency code given in this page to add Maven Dependency of javamail >> mail version1.3.1 in your project

Ads