• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set dellimiters for Email that is saved as a text file in Java?  RSS feed

 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have saved emails that are stored in to text files and I want to retrieve the Sender, Reciever, Subject and the Email Content.
As you can see from the code below, I am using From: as a delimiter and To: as a delimiter and Subject as a delimiter and I am not sure what delimiter I should set for the email content.
Also, there can be emails that will have comments after the message content and I plan on using the delimiter Comments:.

If anyone can give me an example of what delimiters I should use it would be great. Also I would like to know how check whether or not a delimiter exists so that I can print out the comments if it exists
and if the file does not have comments, it prints out nothing.


The email files look somewhat like this:

From: Example@example.com
To: 2ndExample@examile.com
Date: 6/27/2014 10:17 AM
Subject: Subject

hello sir,

this is my email content


from.
Abhi



 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From: Example@example.com
To: 2ndExample@examile.com
Date: 6/27/2014 10:17 AM
Subject: Subject

hello sir,

this is my email content


from.
Abhi


You can extract as follows

From - from beginning to indexOf To
To - from indexOf To to indexOf Date
Date - from indexOf Date to indexOf Subject
Subject - from indexOf Subject to one line blank

rest is body

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abh Nai wrote: . . .
From: Example@example.com
To: 2ndExample@examile.com
Date: 6/27/2014 10:17 AM
Subject: Subject

hello sir,

this is Abh Nai's email content:-
Forwarded From: campbell.ritchie@some-email.com
Forwarded To: abh.nai@other-email.com
Forwarded Date: 23/7/2014 12:34:56am
Forwarded Subject: to confirmation

That program is really good

from.
Abhi
. . .
What will happen when you parse that sort of message?
Why are you using Java to parse it?

And welcome to the Ranch
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you show the implementation for the subject and also how should I check whether the delimiter Comments: exists or not?
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my actual project I will store these values as parameter to the email object which will appear on a JTable.

I am just using this code to see if this works
This is how far I go so far.
I'm not sure how to parse the subject.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to show us the definitive format for the input files. How do you know there will be no duplication as I showed you?
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the format is some what like this
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example of a reply. I don't mind the reply being in the message content.

 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there another way to parse files without java?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abh Nai wrote:Hello, I have saved emails that are stored in to text files and I want to retrieve the Sender, Reciever, Subject and the Email Content.
As you can see from the code below, I am using From: as a delimiter and To: as a delimiter and Subject as a delimiter and I am not sure what delimiter I should set for the email content.

I have two suggestions:
1. Don't think of these things as "e-mails". They are files, or pieces of text, and they have a format. What you need to do is work out what it is (and hopefully tell us).
2. This code should NOT be in an actionPerformed() method, because display or "reaction" has NOTHING to do with your problem.

If you need to be able to "get" these pieces of information from your file, then you need to write code that can do that for you, and forget all about what "mouse clicks" are occurring - and my suggestion would be to create a completely separate class (Email?) whose sole business is to deal with your "e-mail contents"; and that class should not even have a sniff of a 'J' class (JFrame, JContainer etc) in it.

HIH

Winston
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abh,
When you say you have saved the emails
in files, do you mean each file contains one
message? If that is the case, you could easily
parse it. But if there are many email messages
in one file, it will be difficult to process it.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my actual program I have an email event class that has a method that does this. it used similar delimiter to the code I put into the actionevent. That method takes in a File as a parameter and attempts to parse the information. However, it is not parsing properly. Would you like me to post the code for that class?
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Ngom

There is only one email in every file but the file may show the reply message or the forwarded message. I gave examples of reply and the forwarded messages. I do not mind these messages being part of the message content.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then,
Why don't you do four sequential reads on
you scanner object(scanner.next()). First,
you retrieve the From value, and so on
and you make a loop from the fifth sequential
read up to the end of the file to retrieve the
body of message.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use += on Strings inside a loop because of potentially poor performance. Use a StringBuilder and its append() method. That way you can get faster performance and append LINE_END too.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the late response. I updated the code to this but now my program is won't run when I retrieve the file.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abh Nai wrote:I updated the code to this but now my program is won't run when I retrieve the file.

Just saying it doesn't work doesn;t help us to help you to fix the problem, please TellTheDetails (← click)
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above code, you are moving the scanner to go past the line you wanted to split. Try something like this:

you should return the text after the first occurence of the colon. You can do the same for the rest(to, cc,date and subject). Date is a reserved word, so change the name to something else. You should also append a line feed to the end of the line you are appending to string like this:

I hope you have thrown the IOException and FileNotFoundException in your main declaration.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote: . . . Date is a reserved word, so change the name to something else. . . .
No, Date is not a reserved word in Java®. Please explain what you meant by that.

I think you may suffer errors if you have a line like
I have made a booking From: 24th July To: 29th July
I think you may have to revise that suggestion.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No, Date is not a reserved word in Java®.

I Agree. Thanks Campbell for correcting me.

I think you may suffer errors if you have a line like
I have made a booking From: 24th July To: 29th July
I think you may have to revise that suggestion.

He knows the formats in the file already. So he will not have such a phrase appearing as first line in file. He could even ignore that test. Same applies to TO, CC, Date. and Subject.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote: . . .
He knows the formats in the file already. . . .
He has only told us what the formats somewhat are.
The code you posted does not show any sign of being restricted to the first line, so I cannot tell whether such text would be a problem or not.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code you posted does not show any sign of being restricted to the first line, so I cannot tell whether such text would be a problem or not.

That is the very first line returned from scanner in file.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it doesn't say so in your post. If you are restricting that to the first line then you are all right. If the first line is blank, then you will have problems.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the first line is blank, then you will have problems.

If it is blank then it won't have a From value. I think Abh assumes the file has the appropriate formats otherwise there are so many conditions that could make the program not to return the correct values. Let's wait to hear from him.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emails will have a From, To,date, Subject, and message content. The email can have a CC, and attachments as follows:



As of now I am not interested in the attachments because I will later in my actual program ask the user to enter in the file path where the attachment has been saved and allow them to be viewed in a list. I am going to test out Paul's code. I apologize for the late response I had to change my actual program completely.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will move the Date: tag into a different line. So you will have to modify the code slightly to account for tha.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to test this code out. this is from my actual project

 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To line should actually look like this. I tested the code and it seems to work. What do you guys think?

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will only work if you are sure there is no colon before where you want it. It will probably return the line unchanged it you have no colon.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not know whether the colon follows From or not.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The email files i'm using will always have a colon after the delimiter. I'm not quite following what you mean.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be sure there is no colon before the delimiter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abh Nai wrote:The email files i'm using will always have a colon after the delimiter.

Actually, I reckon that the colon IS the delimiter for those "header-type" things.

Like I say, forget about files and actions and what-have-you and deal with the content.

The problem as I see it is this:
  • You have email text supplied in "lines".
  • The first (few) of those lines contains header information in the form:

    label:[possibly some whitespace]data
     
  • The body of the email is separated from those header lines by at least one blank line.

  • I suspect that you will always have at least two header lines ("from:" and "to:"), but TBH, I don't know enough about email specifications to be sure.

    Now, write a class, or method (but I think a class would be better), to solve THAT.

    Right now, you seem to be obsessing about how to do everything, when what you should be concentrating on is WHAT you need to do - ie, a clear, concise description of your requirements in English (or your native language).

    The above hopefully gives you something to work from, but it's almost certainly not complete, so you have to fill in the blanks.

    You might also want to look at the StopCoding (←click→) and WhatNotHow pages.

    HIH

    Winston
     
    Abh Nai
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you suggesting I create 2 string builder objects one for the header and the other for message content and create a method to parse the header string builder object?
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No.
    Winston is suggesting you write down the real specification for the files you are parsing and work out with a pencil and paper how you are going to parse them. Don't write any names of Java classes.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Abh Nai wrote:Are you suggesting I create 2 string builder objects one for the header and the other for message content and create a method to parse the header string builder object?

    Possibly, but you're still worrying about the how. What you need to do is to write code that can handle any kind or number of "header" lines (presumably in a way that allows you to retrieve their values), and keep them separate from the email body. Now, whether you use a StringBuilder or a tokenizer or String.split(), or something completely different to do it is completely immaterial at this stage.

    I'll say it again: Understand WHAT you need to do before you start worrying about how you're going to do it.

    Winston
     
    Abh Nai
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The articles you wrote made things more clearer. Thanks for the advice. I will see what I can do with it.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Abh Nai wrote:The articles you wrote made things more clearer. Thanks for the advice. I will see what I can do with it.

    You're most welcome. Hope it helps.

    And a final tip for you: Code is the last thing you do when writing a program/class; and the most difficult thing to change if you get your design wrong. Design is what; code is how.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!