No EML
Hello Friends
How to read a EML file and
and the Entire content i want to write in a TXT file
the following is my code
BufferedReader in = new BufferedReader(new FileReader(path+"Email"+File.separator+"AMA Report.eml"));
BufferedWriter outing = new BufferedWriter(new FileWriter(path+"Email"+File.separator+"AMA Report2.txt"));
for(int s=0;(m=in.readLine()) != null;s++)
{
if(m.equals(""))
{
s++;
}
else
{
z = m;
outing.write(z);
}
}
outing.close();
i can write the entire content in a TXT file but special characters like nbsp; =20 ; = all such types are adding in the lines and not displaying correctly
so what to do
Help me
Thanks
Message was edited by:
Chilakala
EML are saved email messages from Outlook, but other email apps use it as well
reading the file directly is too much trouble... searching by "java library eml" would help you: use JavaMail - http://java.sun.com/products/javamail/
some sample code:
http://www.rgagnon.com/javadetails/java-0458.html
you can also construct the MimeMessage object using InputStream, useful for your code
mlk: a useful site for such doubts is www.filext.com
you should take a quick look at some JavaMail tutorial, to get yourself familiarized with the API...
I never used JavaMail, but I guess this should work:
Message msg = new MimeMessage(null, new FileInputStream("yourmailfile.eml"));
Object content = msg.getContent();
> i am getting body content as follows:-
>
> javax.mail.internet.MimeMultipart@d03a4e
>
> How to get the whole Content
first of all, read the docs! From getContent() javadocs:
"Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass. For content-types that are unknown to the DataHandler system, an input stream is returned as the content"
that's a lot helpful, so if you want the text of this content, use something like this:
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
Multipart mpContent = (Multipart)content;
for (int part = 0; part < mpContent.getCount(); part++) {
System.out.println(mpContent.getBodyPart(part).getContent());
//much probably will need recursive call to hadler method!
}
} else if (content instanceof InputStream) {
InputStream is = (InputStream)content;
//use it somehow
} else {
//unexpected situation
}
I think this code might work.
Anyway... the answers are pretty obvious... you should learn to look for them yourself!
There will not be someone around with enough time to code things for you everytime you face a little problem...
If you are really a Java beginner, maybe you should start coding simpler things, and take some time to read a good tutorial, like Java Tutorial from Sun.
Hello dev@java ,
How are you
really u r Helping a lot
i am having a small doubt
in the below code what is the purpose of connecting to mail server.
He is giving abosolute path only.
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class ReadEmail {
public static void main(String args[]) throws Exception{
display(new File("C:\\temp\\message.eml"));
}
public static void display(File emlFile) throws Exception{
Properties props = System.getProperties();
props.put("mail.host", "smtp.dummydomain.com");
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream(emlFile);
MimeMessage message = new MimeMessage(mailSession, source);
System.out.println("Subject : " + message.getSubject());
System.out.println("From : " + message.getFrom()[0]);
System.out.println("--");
System.out.println("Body : " + message.getContent());
}
}
> in the below code what is the purpose of connecting
> to mail server.
I see no purpose either... that's why I posted it with a null session. The API does not specify what's the behavior in this case, but I guess it might work
That's something bothered me: the JavaMail docs are not fully detailed like core Java docs
> He is giving abosolute path only.
what?