2 * This class is a container for all data contained in an Email Message.
6 String header; // the full header
7 //String sentOn; // time the message was sent
8 //String receivedOn; // time when the message arrived
9 String from; // the "from" field
10 String to; // the "to" field
17 boolean hasAttachement;
18 String encoding; //rich text, plain, html
20 String messageID; // cached message ID for reuse (takes a lot of memory and is used all over the place)
21 //same as hashcode of a class
25 * this is a really simple implementation of a tokenizer
26 * used to build tokens from an email and divide email into parts
34 public Mail(String fileName) // read a mail from file
36 System.out.println("fileName= " + fileName);
38 FileInputStream fileinput = new FileInputStream(fileName);
42 while((line = fileinput.readLine()) != null)
46 Vector splittedLine = line.split();
47 if(((String)(splittedLine.elementAt(0))).equals("Spam:"))
49 spam = (String)(splittedLine.elementAt(1));
51 else if(((String)(splittedLine.elementAt(0))).equals("Header:")) // message id
53 header = (String)splittedLine.elementAt(1);
55 else if(((String)(splittedLine.elementAt(0))).equals("To:")) // receiver
57 to = (String)splittedLine.elementAt(1);
59 else if(((String)(splittedLine.elementAt(0))).equals("From:")) // sender
61 from = (String)splittedLine.elementAt(1);
63 else if(((String)(splittedLine.elementAt(0))).equals("Cc:")) // cc
65 cc = (String)splittedLine.elementAt(1);
67 else if(((String)(splittedLine.elementAt(0))).equals("Title:")) // Subject
69 subject = (String)splittedLine.elementAt(1);
72 } // parsed messageID, To, from, cc, Title
78 System.out.println("no line read");
82 byte[] readBody = new byte[256];
84 while((fileinput.read(readBody)>0))
86 body += new String(readBody);
91 MAX_TOKEN_SIZE = 1024;
94 // -------------------------------------------------------
96 public void setHeader(String header) {
100 public String getHeader() {
106 public void setSentOn(String sentOn) {
107 this.sentOn = sentOn;
110 public String getSentOn() {
114 public Date getSentOnAsDate() {
115 String sentOn = getSentOn();
116 return parseDate(sentOn);
119 public void setReceivedOn(String receivedOn) {
120 this.receivedOn = receivedOn;
123 public String getReceivedOn() {
127 public Date getReceivedOnAsDate() {
128 String receivedOn = getReceivedOn();
129 return parseDate(receivedOn);
135 * Parses a given Date-String in into a real Date-Object
137 * @param stringDate the string in format dd.mm.yyyy hh:mm
138 * @return a Date containing the info of the string or the actual date and time if something fails.
141 public Date parseDate(String stringDate) {
142 // date is in this format: dd.mm.yyyy hh:mm
143 if (stringDate == null || "N/A".equals(stringDate)) {
147 synchronized (MAIL_TIME_FORMAT) {
148 return MAIL_TIME_FORMAT.parse(stringDate);
150 } catch (Throwable e) {
156 public void setFrom(String from) {
160 public String getFrom() {
164 public void setTo(String to) {
168 public String getTo() {
172 public void setCc(String cc) {
176 public String getCc() {
180 public void setSubject(String subject) {
181 this.subject = subject;
184 public String getSubject() {
188 public void setBody(String body) {
192 public String getBody() {
196 public void setSourceCode(String sourceCode) {
197 this.sourceCode = sourceCode;
200 public String getSourceCode() {
204 public void setHasAttachement(boolean hasAttachement) {
205 this.hasAttachement = hasAttachement;
208 public boolean getHasAttachement() {
209 return hasAttachement;
212 public void setEncoding(String encoding) {
213 this.encoding = encoding;
216 public String getEncoding() {
220 public boolean isTextEncoding() {
221 return getEncoding().toLowerCase().indexOf("plain") >= 0;
224 public boolean isHTMLEncoding() {
225 return getEncoding().toLowerCase().indexOf("html") >= 0;
229 public String toString() {
230 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
234 public String toString() {
235 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
239 public String getID() {
240 if (messageID == null) { // no cached version
241 // Take the message-ID header as ID (if present)
242 String[] messageIDs = getHeaderField("Message-ID");
243 if ((messageIDs != null) && (messageIDs.length > 0)) {
244 messageID = messageIDs[0];
245 } else { // otherwise, hash header and body as ID
246 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
254 public String[] getHeaderField(String fieldName) {
258 public String extractEMailAddress() {
263 public boolean equals(Object o) {
264 if (o instanceof Mail) {
266 return this.getID().equals(mail.getID());
273 public Vector getCommonPart()
275 Vector returnStrings = new Vector();
277 // add header, sender, and title
278 returnStrings.addElement(header);
279 returnStrings.addElement(from);
280 returnStrings.addElement(subject);
282 return returnStrings;
285 public String getBodyString()
290 public Vector returnEmail() {
291 Vector myemail = new Vector();
292 myemail.addElement(getCommonPart());
293 //System.out.println("DEBUG: getCommonPart.size= " + getCommonPart().size());
294 myemail.addElement(getURLs());
295 //System.out.println("DEBUG: getURLs.size= " + getURLs().size());
296 myemail.addElement(getSplittedBody(MAX_TOKEN_SIZE));
297 //System.out.println("DEBUG: getSplittedBody.size= " + getSplittedBody(MAX_TOKEN_SIZE).size());
301 public Vector getURLs()
303 Vector returnStrings = new Vector();
304 Vector splittedBody = body.split();
306 // add URL and email in the body
307 for(int i=0; i<splittedBody.size(); i++)
309 String segment = (String)(splittedBody.elementAt(i));
310 if(segment.startsWith("http://")) // URL
312 returnStrings.addElement(segment);
314 else if(isEmailAccount(segment)) // email
316 returnStrings.addElement(segment);
320 return returnStrings;
323 // check if it is email account string
324 private boolean isEmailAccount(String str)
326 if(str.contains("@") && str.contains("."))
332 public void setNoURLBody()
334 noURLBody = new String();
336 Vector splittedBody = body.split();
338 //System.out.println("DEBUG: splittedBody.size()= " + splittedBody.size());
339 for(int i=0; i< splittedBody.size();i ++)
341 String segment = (String)(splittedBody.elementAt(i));
342 //System.out.println("DEBUG: segment= " + segment);
344 if(!(segment.startsWith("http://") || isEmailAccount(segment)))
345 noURLBody += segment;
349 // setNoURLBody method has to be called before this method
350 // parameter : bytesize to split.
351 public Vector getSplittedBody(int size)
354 Vector returnStrings = new Vector();
356 char[] charArray = noURLBody.toCharArray();
358 String tmpStr = new String();
359 tmpStr += charArray[0];
361 //System.out.println("tmpStr= " + tmpStr);
363 for(int i=1; i< noURLBody.length(); i++)
365 if((i % size) == 0) {
366 returnStrings.addElement(tmpStr);
367 tmpStr = new String();
370 tmpStr += charArray[i];
374 returnStrings.addElement(tmpStr);
376 return returnStrings;
380 public void setIsSpam(boolean spam) {
384 public boolean getIsSpam() {
385 if(spam.equals("yes"))
391 * Returns result to the Spam filter
393 public Vector checkMail(int userid) {
395 //Vector partsOfMailStrings = mail.createMailStringsWithURL();
396 //Vector partsOfMailStrings = getCommonPart();
397 //partsOfMailStrings.addElement(getBodyString());
398 Vector partsOfMailStrings = returnEmail();
402 SignatureComputer sigComp = new SignatureComputer();
403 //Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
404 Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of vector of strings
411 public static void main(String[] args)
413 Mail mail = new Mail("./emails/email1");
415 String[] a = mail.createMailStrings();
419 System.out.println(b);