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
69 System.out.println("no line read");
74 while((line = fileinput.readLine()) != null)
82 // -------------------------------------------------------
84 public void setHeader(String header) {
88 public String getHeader() {
94 public void setSentOn(String sentOn) {
98 public String getSentOn() {
102 public Date getSentOnAsDate() {
103 String sentOn = getSentOn();
104 return parseDate(sentOn);
107 public void setReceivedOn(String receivedOn) {
108 this.receivedOn = receivedOn;
111 public String getReceivedOn() {
115 public Date getReceivedOnAsDate() {
116 String receivedOn = getReceivedOn();
117 return parseDate(receivedOn);
123 * Parses a given Date-String in into a real Date-Object
125 * @param stringDate the string in format dd.mm.yyyy hh:mm
126 * @return a Date containing the info of the string or the actual date and time if something fails.
129 public Date parseDate(String stringDate) {
130 // date is in this format: dd.mm.yyyy hh:mm
131 if (stringDate == null || "N/A".equals(stringDate)) {
135 synchronized (MAIL_TIME_FORMAT) {
136 return MAIL_TIME_FORMAT.parse(stringDate);
138 } catch (Throwable e) {
144 public void setFrom(String from) {
148 public String getFrom() {
152 public void setTo(String to) {
156 public String getTo() {
160 public void setCc(String cc) {
164 public String getCc() {
168 public void setSubject(String subject) {
169 this.subject = subject;
172 public String getSubject() {
176 public void setBody(String body) {
180 public String getBody() {
184 public void setSourceCode(String sourceCode) {
185 this.sourceCode = sourceCode;
188 public String getSourceCode() {
192 public void setHasAttachement(boolean hasAttachement) {
193 this.hasAttachement = hasAttachement;
196 public boolean getHasAttachement() {
197 return hasAttachement;
200 public void setEncoding(String encoding) {
201 this.encoding = encoding;
204 public String getEncoding() {
208 public boolean isTextEncoding() {
209 return getEncoding().toLowerCase().indexOf("plain") >= 0;
212 public boolean isHTMLEncoding() {
213 return getEncoding().toLowerCase().indexOf("html") >= 0;
217 public String toString() {
218 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
222 public String toString() {
223 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
227 public String getID() {
228 if (messageID == null) { // no cached version
229 // Take the message-ID header as ID (if present)
230 String[] messageIDs = getHeaderField("Message-ID");
231 if ((messageIDs != null) && (messageIDs.length > 0)) {
232 messageID = messageIDs[0];
233 } else { // otherwise, hash header and body as ID
234 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
242 public String[] getHeaderField(String fieldName) {
246 public String extractEMailAddress() {
251 public boolean equals(Object o) {
252 if (o instanceof Mail) {
254 return this.getID().equals(mail.getID());
261 public Vector getCommonPart()
263 Vector returnStrings = new Vector();
265 // add header, sender, and title
266 returnStrings.addElement(header);
267 returnStrings.addElement(from);
268 returnStrings.addElement(subject);
270 return returnStrings;
273 public String getBodyString()
278 /* TODO add this to process entire email
279 public Vector returnEmail() {
280 Vector myemail = new Vector();
282 myemail.addElement(getCommonPart());
283 myemail.addElement(getURLs());
284 myemail.addElement(getSplittedBody());
289 public Vector getURLs()
291 Vector returnStrings = new Vector();
292 Vector splittedBody = body.split();
294 // add URL and email in the body
295 for(int i=0; i<splittedBody.size(); i++)
297 String segment = (String)splittedBody.elementAt(i);
298 if(segment.startsWith("http://")) // URL
300 returnStrings.addElement(segment);
302 else if(isEmailAccount(segment)) // email
304 returnStrings.addElement(segment);
308 return returnStrings;
311 // check if it is email account string
312 private boolean isEmailAccount(String str)
314 if(str.contains("@") && str.contains("."))
320 public void setNoURLBody()
322 noURLBody = new String();
323 Vector splittedBody = body.split();
325 for(int i=0; i< splittedBody.size();i ++)
327 String segment = (String)splittedBody.elementAt(i);
329 if(!(segment.startsWith("http://") || isEmailAccount(segment)))
330 noURLBody += segment;
334 // setNoURLBody method has to be called before this method
335 // parameter : bytesize to split.
336 public Vector getSplittedBody(int size)
339 Vector returnStrings = new Vector();
341 char[] charArray = noURLBody.toCharArray();
343 String tmpStr = new String();
344 tmpStr += charArray[0];
346 for(int i=1; i< noURLBody.length(); i++)
348 if((i % size) == 0) {
349 returnStrings.addElement(tmpStr);
350 tmpStr = new String();
353 tmpStr += charArray[i];
357 returnStrings.addElement(tmpStr);
359 return returnStrings;
363 public void setIsSpam(boolean spam) {
367 public boolean getIsSpam() {
368 if(spam.equals("yes"))
374 * Returns result to the Spam filter
376 public Vector checkMail(int userid) {
378 //Vector partsOfMailStrings = mail.createMailStringsWithURL();
379 Vector partsOfMailStrings = getCommonPart();
380 //partsOfMailStrings.addElement(getBodyString());
383 SignatureComputer sigComp = new SignatureComputer();
384 Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
391 public static void main(String[] args)
393 Mail mail = new Mail("./emails/email1");
395 String[] a = mail.createMailStrings();
399 System.out.println(b);