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
28 public Mail(String fileName) // read a mail from file
30 //System.out.println("fileName= " + fileName);
32 FileInputStream fileinput = new FileInputStream(fileName);
36 while((line = fileinput.readLine()) != null)
40 Vector splittedLine = line.split();
41 if(((String)(splittedLine.elementAt(0))).equals("Spam:"))
43 spam = (String)splittedLine.elementAt(1);
45 else if(((String)(splittedLine.elementAt(0))).equals("Header:")) // message id
47 header = (String)splittedLine.elementAt(1);
49 else if(((String)(splittedLine.elementAt(0))).equals("To:")) // receiver
51 to = (String)splittedLine.elementAt(1);
53 else if(((String)(splittedLine.elementAt(0))).equals("From:")) // sender
55 from = (String)splittedLine.elementAt(1);
57 else if(((String)(splittedLine.elementAt(0))).equals("Cc:")) // cc
59 cc = (String)splittedLine.elementAt(1);
61 else if(((String)(splittedLine.elementAt(0))).equals("Title:")) // Subject
63 subject = (String)splittedLine.elementAt(1);
66 } // parsed messageID, To, from, cc, Title
70 System.out.println("no line read");
74 byte[] readBody = new byte[256];
76 while((fileinput.read(readBody)>0))
78 body.concat(new String(readBody));
84 // -------------------------------------------------------
86 public void setHeader(String header) {
90 public String getHeader() {
96 public void setSentOn(String sentOn) {
100 public String getSentOn() {
104 public Date getSentOnAsDate() {
105 String sentOn = getSentOn();
106 return parseDate(sentOn);
109 public void setReceivedOn(String receivedOn) {
110 this.receivedOn = receivedOn;
113 public String getReceivedOn() {
117 public Date getReceivedOnAsDate() {
118 String receivedOn = getReceivedOn();
119 return parseDate(receivedOn);
125 * Parses a given Date-String in into a real Date-Object
127 * @param stringDate the string in format dd.mm.yyyy hh:mm
128 * @return a Date containing the info of the string or the actual date and time if something fails.
131 public Date parseDate(String stringDate) {
132 // date is in this format: dd.mm.yyyy hh:mm
133 if (stringDate == null || "N/A".equals(stringDate)) {
137 synchronized (MAIL_TIME_FORMAT) {
138 return MAIL_TIME_FORMAT.parse(stringDate);
140 } catch (Throwable e) {
146 public void setFrom(String from) {
150 public String getFrom() {
154 public void setTo(String to) {
158 public String getTo() {
162 public void setCc(String cc) {
166 public String getCc() {
170 public void setSubject(String subject) {
171 this.subject = subject;
174 public String getSubject() {
178 public void setBody(String body) {
182 public String getBody() {
186 public void setSourceCode(String sourceCode) {
187 this.sourceCode = sourceCode;
190 public String getSourceCode() {
194 public void setHasAttachement(boolean hasAttachement) {
195 this.hasAttachement = hasAttachement;
198 public boolean getHasAttachement() {
199 return hasAttachement;
202 public void setEncoding(String encoding) {
203 this.encoding = encoding;
206 public String getEncoding() {
210 public boolean isTextEncoding() {
211 return getEncoding().toLowerCase().indexOf("plain") >= 0;
214 public boolean isHTMLEncoding() {
215 return getEncoding().toLowerCase().indexOf("html") >= 0;
219 public String toString() {
220 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
224 public String toString() {
225 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
229 public String getID() {
230 if (messageID == null) { // no cached version
231 // Take the message-ID header as ID (if present)
232 String[] messageIDs = getHeaderField("Message-ID");
233 if ((messageIDs != null) && (messageIDs.length > 0)) {
234 messageID = messageIDs[0];
235 } else { // otherwise, hash header and body as ID
236 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
244 public String[] getHeaderField(String fieldName) {
248 public String extractEMailAddress() {
253 public boolean equals(Object o) {
254 if (o instanceof Mail) {
256 return this.getID().equals(mail.getID());
263 public Vector getCommonPart()
265 Vector returnStrings = new Vector();
267 // add header, sender, and title
268 returnStrings.addElement(header);
269 returnStrings.addElement(from);
270 returnStrings.addElement(subject);
272 return returnStrings;
275 public String getBodyString()
280 /* TODO add this to process entire email
281 public Vector returnEmail() {
282 Vector myemail = new Vector();
284 myemail.addElement(getCommonPart());
285 myemail.addElement(getURLs());
286 myemail.addElement(getSplittedBody());
291 public Vector getURLs()
293 Vector returnStrings = new Vector();
294 Vector splittedBody = body.split();
296 // add URL and email in the body
297 for(int i=0; i<splittedBody.size(); i++)
299 String segment = (String)splittedBody.elementAt(i);
300 if(segment.startsWith("http://")) // URL
302 returnStrings.addElement(segment);
304 else if(isEmailAccount(segment)) // email
306 returnStrings.addElement(segment);
310 return returnStrings;
313 // check if it is email account string
314 private boolean isEmailAccount(String str)
316 if(str.contains("@") && str.contains("."))
322 public void setNoURLBody()
324 noURLBody = new String();
325 Vector splittedBody = body.split();
327 for(int i=0; i< splittedBody.size();i ++)
329 String segment = (String)splittedBody.elementAt(i);
331 if(!(segment.startsWith("http://") || isEmailAccount(segment)))
332 noURLBody += segment;
336 // setNoURLBody method has to be called before this method
337 // parameter : bytesize to split.
338 public Vector getSplittedBody(int size)
341 Vector returnStrings = new Vector();
343 char[] charArray = noURLBody.toCharArray();
345 String tmpStr = new String();
346 tmpStr += charArray[0];
348 for(int i=1; i< noURLBody.length(); i++)
350 if((i % size) == 0) {
351 returnStrings.addElement(tmpStr);
352 tmpStr = new String();
355 tmpStr += charArray[i];
359 returnStrings.addElement(tmpStr);
361 return returnStrings;
365 public void setIsSpam(boolean spam) {
369 public boolean getIsSpam() {
370 if(spam.equals("yes"))
376 * Returns result to the Spam filter
378 public Vector checkMail(int userid) {
380 //Vector partsOfMailStrings = mail.createMailStringsWithURL();
381 Vector partsOfMailStrings = getCommonPart();
382 //partsOfMailStrings.addElement(getBodyString());
385 SignatureComputer sigComp = new SignatureComputer();
386 Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
393 public static void main(String[] args)
395 Mail mail = new Mail("./emails/email1");
397 String[] a = mail.createMailStrings();
401 System.out.println(b);