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("DEBUG: fileName= " + fileName);
38 BufferedReader fileinput = new BufferedReader(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("Subject:")) // 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);
87 readBody = new byte[256];
92 MAX_TOKEN_SIZE = 1024;
95 // -------------------------------------------------------
97 public void setHeader(String header) {
101 public String getHeader() {
107 public void setSentOn(String sentOn) {
108 this.sentOn = sentOn;
111 public String getSentOn() {
115 public Date getSentOnAsDate() {
116 String sentOn = getSentOn();
117 return parseDate(sentOn);
120 public void setReceivedOn(String receivedOn) {
121 this.receivedOn = receivedOn;
124 public String getReceivedOn() {
128 public Date getReceivedOnAsDate() {
129 String receivedOn = getReceivedOn();
130 return parseDate(receivedOn);
136 * Parses a given Date-String in into a real Date-Object
138 * @param stringDate the string in format dd.mm.yyyy hh:mm
139 * @return a Date containing the info of the string or the actual date and time if something fails.
142 public Date parseDate(String stringDate) {
143 // date is in this format: dd.mm.yyyy hh:mm
144 if (stringDate == null || "N/A".equals(stringDate)) {
148 synchronized (MAIL_TIME_FORMAT) {
149 return MAIL_TIME_FORMAT.parse(stringDate);
151 } catch (Throwable e) {
157 public void setFrom(String from) {
161 public String getFrom() {
165 public void setTo(String to) {
169 public String getTo() {
173 public void setCc(String cc) {
177 public String getCc() {
181 public void setSubject(String subject) {
182 this.subject = subject;
185 public String getSubject() {
189 public void setBody(String body) {
193 public String getBody() {
197 public void setSourceCode(String sourceCode) {
198 this.sourceCode = sourceCode;
201 public String getSourceCode() {
205 public void setHasAttachement(boolean hasAttachement) {
206 this.hasAttachement = hasAttachement;
209 public boolean getHasAttachement() {
210 return hasAttachement;
213 public void setEncoding(String encoding) {
214 this.encoding = encoding;
217 public String getEncoding() {
221 public boolean isTextEncoding() {
222 return getEncoding().toLowerCase().indexOf("plain") >= 0;
225 public boolean isHTMLEncoding() {
226 return getEncoding().toLowerCase().indexOf("html") >= 0;
230 public String toString() {
231 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
235 public String toString() {
236 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
240 public String getID() {
241 if (messageID == null) { // no cached version
242 // Take the message-ID header as ID (if present)
243 String[] messageIDs = getHeaderField("Message-ID");
244 if ((messageIDs != null) && (messageIDs.length > 0)) {
245 messageID = messageIDs[0];
246 } else { // otherwise, hash header and body as ID
247 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
255 public String[] getHeaderField(String fieldName) {
259 public String extractEMailAddress() {
264 public boolean equals(Object o) {
265 if (o instanceof Mail) {
267 return this.getID().equals(mail.getID());
274 public Vector getCommonPart()
276 Vector returnStrings = new Vector();
278 // add header, sender, and title
279 returnStrings.addElement(header);
280 returnStrings.addElement(from);
281 returnStrings.addElement(subject);
283 return returnStrings;
286 public String getBodyString()
291 public Vector returnEmail() {
292 Vector myemail = new Vector();
293 myemail.addElement(getCommonPart());
294 //System.out.println("DEBUG: getCommonPart.size= " + getCommonPart().size());
295 myemail.addElement(getURLs());
296 //System.out.println("DEBUG: getURLs.size= " + getURLs().size());
297 myemail.addElement(getSplittedBody(MAX_TOKEN_SIZE));
298 //System.out.println("DEBUG: getSplittedBody.size= " + getSplittedBody(MAX_TOKEN_SIZE).size());
302 public Vector getURLs()
304 Vector returnStrings = new Vector();
305 Vector splittedBody = body.split();
307 // add URL and email in the body
308 for(int i=0; i<splittedBody.size(); i++)
310 String segment = (String)(splittedBody.elementAt(i));
311 if(segment.startsWith("http://")) // URL
313 returnStrings.addElement(segment);
315 else if(isEmailAccount(segment)) // email
317 returnStrings.addElement(segment);
321 return returnStrings;
324 // check if it is email account string
325 private boolean isEmailAccount(String str)
327 if(str.contains("@") && str.contains("."))
333 public void setNoURLBody()
335 Vector splittedBody = body.split();
337 for(int i=0; i< splittedBody.size();i ++) {
338 String segment = (String)(splittedBody.elementAt(i));
339 if(!(segment.startsWith("http://") || isEmailAccount(segment)))
340 totalsize+=segment.length();
343 StringBuffer sb=new StringBuffer(totalsize);
344 for(int i=0; i< splittedBody.size();i ++) {
345 String segment = (String)(splittedBody.elementAt(i));
346 if(!(segment.startsWith("http://") || isEmailAccount(segment))) {
350 noURLBody=sb.toString();
353 // setNoURLBody method has to be called before this method
354 // parameter : bytesize to split.
355 public Vector getSplittedBody(int size)
358 Vector returnStrings = new Vector();
359 int end=noURLBody.length();
361 for(int i=1; i< end; i+=size)
364 String str=noURLBody.substring(i, end);
365 returnStrings.addElement(str);
368 String str=noURLBody.substring(i, i+size);
369 returnStrings.addElement(str);
372 return returnStrings;
376 public void setIsSpam(boolean spam) {
380 public boolean getIsSpam() {
381 if(spam.equals("yes"))
387 * Returns result to the Spam filter
389 public Vector checkMail(int userid) {
392 //long startGetParts=System.currentTimeMillis();
393 Vector partsOfMailStrings = returnEmail();
394 //long stopGetParts=System.currentTimeMillis();
395 //System.out.println("Time to read email= " + (stopGetParts-startGetParts));
398 SignatureComputer sigComp = new SignatureComputer();
399 //Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
400 //long startGetsignatures=System.currentTimeMillis();
401 Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of vector of strings
402 //long stopGetsignatures=System.currentTimeMillis();
403 //System.out.println("Time to Getsignatures= " + (stopGetsignatures-startGetsignatures));
410 public static void main(String[] args)
412 Mail mail = new Mail("./emails/email1");
414 String[] a = mail.createMailStrings();
418 System.out.println(b);