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
16 boolean hasAttachement;
17 //String encoding; //rich text, plain, html
19 String messageID; // cached message ID for reuse (takes a lot of memory and is used all over the place)
20 //same as hashcode of a class
27 public Mail(String fileName) // read a mail from file
29 FileInputStream fileinput = new FileInputStream(fileName);
32 while((line = fileinput.readLine()) != null)
34 String[] splittedLine = line.split();
35 if(splittedLine[0].equals("Header:")) // message id
37 header = splittedLine[1];
39 else if(splittedLine[0].equals("To:")) // receiver
43 else if(splittedLine[0].equals("From:")) // sender
45 from = splittedLine[1];
47 else if(splittedLine[0].equals("Cc:")) // cc
51 else if(splittedLine[0].equals("Title:")) // Subject
53 subject = splittedLine[1];
56 } // parsed messageID, To, from, cc, Title
60 while((line = fileinput.readLine()) != null)
67 // -------------------------------------------------------
69 public void setHeader(String header) {
73 public String getHeader() {
77 public void setSentOn(String sentOn) {
81 public String getSentOn() {
86 public Date getSentOnAsDate() {
87 String sentOn = getSentOn();
88 return parseDate(sentOn);
91 public void setReceivedOn(String receivedOn) {
92 this.receivedOn = receivedOn;
95 public String getReceivedOn() {
99 public Date getReceivedOnAsDate() {
100 String receivedOn = getReceivedOn();
101 return parseDate(receivedOn);
106 * Parses a given Date-String in into a real Date-Object
108 * @param stringDate the string in format dd.mm.yyyy hh:mm
109 * @return a Date containing the info of the string or the actual date and time if something fails.
112 public Date parseDate(String stringDate) {
113 // date is in this format: dd.mm.yyyy hh:mm
114 if (stringDate == null || "N/A".equals(stringDate)) {
118 synchronized (MAIL_TIME_FORMAT) {
119 return MAIL_TIME_FORMAT.parse(stringDate);
121 } catch (Throwable e) {
127 public void setFrom(String from) {
131 public String getFrom() {
135 public void setTo(String to) {
139 public String getTo() {
143 public void setCc(String cc) {
147 public String getCc() {
151 public void setSubject(String subject) {
152 this.subject = subject;
155 public String getSubject() {
159 public void setBody(String body) {
163 public String getBody() {
167 public void setSourceCode(String sourceCode) {
168 this.sourceCode = sourceCode;
171 public String getSourceCode() {
175 // TODO: String? Is this a boolean, a number, or can be both?
176 public void setHasAttachement(boolean hasAttachement) {
177 this.hasAttachement = hasAttachement;
180 public boolean getHasAttachement() {
181 return hasAttachement;
184 public void setEncoding(String encoding) {
185 this.encoding = encoding;
188 public String getEncoding() {
192 public boolean isTextEncoding() {
193 return getEncoding().toLowerCase().indexOf("plain") >= 0;
196 public boolean isHTMLEncoding() {
197 return getEncoding().toLowerCase().indexOf("html") >= 0;
200 public String toString() {
201 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
205 public String getID() {
206 if (messageID == null) { // no cached version
207 // Take the message-ID header as ID (if present)
208 String[] messageIDs = getHeaderField("Message-ID");
209 if ((messageIDs != null) && (messageIDs.length > 0)) {
210 messageID = messageIDs[0];
211 } else { // otherwise, hash header and body as ID
212 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
220 public String[] getHeaderField(String fieldName) {
224 public String extractEMailAddress() {
228 public boolean equals(Object o) {
229 if (o instanceof Mail) {
231 return this.getID().equals(mail.getID());
237 public Vector createMailStringsWithURL()
239 Vector returnStrings = new Vector();
241 // add header, sender, and title
242 returnStrings.add(header);
243 returnStrings.add(from);
244 returnStrings.add(subject);
246 String[] splittedBody = body.split();
248 // add URL and email in the body
249 for(String segment : splittedBody)
251 if(segment.startsWith("http://")) // URL
253 returnStrings.add(segment);
255 else if(isEmailAccount(segment)) // email
257 returnStrings.add(segment);
261 return returnStrings;
264 // check if it is email account string
265 private boolean isEmailAccount(String str)
267 if(str.contains("@") && str.contains("."))
273 public void setNoURLBody()
275 noURLBody = new String();
276 Vector splittedBody = body.split();
278 for(int i=0; i< splittedBody.size();i ++)
280 String segment = splittedBody.elementAt(i);
282 if(!(segment.startsWith("http://") || isEmailAccount(segment)))
283 noURLBody += segment;
287 // setNoURLBody method has to be called before this method
288 // parameter : bytesize to split.
289 public Vector createMailStringsWithoutURL(int size)
292 Vector returnStrings = new Vector();
294 // add header, sender, and title
295 returnStrings.add(header);
296 returnStrings.add(from);
297 returnStrings.add(subject);
299 char[] charArray = noURLBody.toCharArray();
301 String tmpStr = new String();
302 tmpStr += charArray[0];
304 for(int i=1; i< noURLBody.length(); i++)
306 if((i % size) == 0) {
307 returnStrings.add(tmpStr);
308 tmpStr = new String();
311 tmpStr += charArray[i];
315 returnStrings.add(tmpStr);
317 return returnStrings;
321 public void setIsSpam(boolean spam) {
325 public boolean getIsSpam() {
329 public static void main(String[] args)
331 Mail mail = new Mail("./emails/email1");
333 String[] a = mail.createMailStrings();
337 System.out.println(b);