KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > communication > EMailReceiver


1 /*
2  * Created on 29.03.2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.contineo.communication;
8
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import javax.mail.Address JavaDoc;
15 //import javax.mail.Flags;
16
import javax.mail.Folder JavaDoc;
17 import javax.mail.MessagingException JavaDoc;
18 import javax.mail.Multipart JavaDoc;
19 import javax.mail.Part JavaDoc;
20 import javax.mail.Session JavaDoc;
21 import javax.mail.Store JavaDoc;
22 import javax.mail.internet.InternetAddress JavaDoc;
23
24 import org.contineo.communication.dao.EMailDAO;
25 import org.contineo.core.FileBean;
26 import org.contineo.core.config.SettingConfigurator;
27
28 /**
29  * @author Michael Scholz
30  *
31  * To change the template for this generated type comment go to
32  * Window>Preferences>Java>Code Generation>Code and Comments
33  */

34 public class EMailReceiver {
35
36     /**
37      *
38      * @uml.property name="email"
39      * @uml.associationEnd
40      * @uml.property name="email" multiplicity="(1 1)"
41      */

42     private EMail email;
43
44     private int partCount;
45     private String JavaDoc userdir;
46     
47     /**
48      *
49      */

50     public EMailReceiver() {
51         email = new EMail();
52         partCount = 0;
53         userdir = "";
54     }
55     
56     public void receive(EMailAccount account, String JavaDoc username) throws Exception JavaDoc {
57         EMailDAO emailDao = new EMailDAO();
58         // Connect to POP3-Server
59
Session JavaDoc sess = Session.getDefaultInstance(new Properties JavaDoc());
60         Store JavaDoc store = sess.getStore(account.getProvider());
61         store.connect(account.getHost(), account.getAccountUser(), account.getAccountPassword());
62                     
63         // Open Folder INBOX
64
Folder JavaDoc inbox = store.getFolder("INBOX");
65         if (inbox != null) {
66             inbox.open(Folder.READ_WRITE);
67                         
68             // fetch messages from server
69
javax.mail.Message JavaDoc[] messages = inbox.getMessages();
70             for (int i=0; i<messages.length; i++) {
71                 javax.mail.Message JavaDoc message = messages[i];
72                 Part JavaDoc messagePart = message;
73                 Object JavaDoc content = messagePart.getContent();
74                 InternetAddress JavaDoc from = ((InternetAddress JavaDoc)message.getFrom()[0]);
75                 Address JavaDoc recipients[] = message.getAllRecipients();
76                             
77                 // store message in database
78
if (from != null) {
79                     email.setAuthor(from.getPersonal());
80                     email.setAuthorAddress(from.getAddress());
81                 }
82                 for (int j=0; j<recipients.length; j++) {
83                     Address JavaDoc rec = recipients[j];
84                     Recipient recipient = new Recipient();
85                     recipient.setAddress(rec.toString());
86                     email.addRecipient(recipient);
87                 }
88                 email.setSubject(message.getSubject());
89                 email.setRead(0);
90                 email.setUserName(username);
91                 email.setFolder("inbox");
92                 email.setSentDate(String.valueOf(message.getSentDate().getTime()));
93                 emailDao.store(email);
94                 SettingConfigurator conf = new SettingConfigurator();
95                 userdir = conf.getValue("userdir") + "/" + username + "/mails/" + String.valueOf(email.getMessageId());
96                 FileBean.createDir(userdir);
97                 dumpPart(messagePart);
98                 emailDao.store(email);
99                 // TODO: do not delete messages from mailbox by default; make this an option
100
//message.setFlag(Flags.Flag.DELETED, true);
101
}
102             inbox.close(true);
103         }
104         store.close();
105     }
106     
107     private void dumpPart(Part JavaDoc p) throws MessagingException JavaDoc, IOException JavaDoc {
108         if (p.isMimeType("multipart/*")) {
109             Multipart JavaDoc mp = (Multipart JavaDoc)p.getContent();
110             int count = mp.getCount();
111             for (int i=0;i<count;i++) {
112                 dumpPart(mp.getBodyPart(i));
113             }
114         } else {
115             partCount++;
116             Attachment attachment = new Attachment();
117             String JavaDoc cType = p.getContentType();
118             String JavaDoc filename = p.getFileName();
119             if (filename == null || filename.equals("")) {
120                 filename = "email";
121                 if (cType.startsWith("text/plain"))
122                     filename += ".mail";
123                 if (cType.startsWith("text/html"))
124                     filename += ".html";
125             }
126             int end = cType.indexOf(";");
127             String JavaDoc mimeType = "";
128             if (end != -1)
129                 mimeType = cType.substring(0, cType.indexOf(";"));
130             else
131                 mimeType = cType;
132             InputStream JavaDoc is = p.getInputStream();
133             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(userdir + "/" + filename);
134             int letter = 0;
135             while ((letter = is.read()) != -1) {
136                 fos.write(letter);
137             }
138             is.close();
139             fos.close();
140             String JavaDoc icon = "pages/images/";
141             if (mimeType.equals("text/plain") || mimeType.equals("text/rtf") || mimeType.equals("application/msword") || mimeType.equals("application/vnd.sun.xml.writer"))
142                 icon += "textdoc.gif";
143             else
144                 if (mimeType.equals("application/msexcel") || mimeType.equals("application/vnd.sun.xml.calc"))
145                     icon += "tabledoc.gif";
146                 else
147                     if (mimeType.equals("application/mspowerpoint") || mimeType.equals("application/vnd.sun.xml.impress"))
148                         icon += "presentdoc.gif";
149                     else
150                         if (mimeType.equals("application/pdf"))
151                             icon += "pdf.gif";
152                         else
153                             if (mimeType.equals("text/html"))
154                                 icon += "internet.gif";
155                             else
156                                 icon += "document.gif";
157             attachment.setIcon(icon);
158             attachment.setMimeType(mimeType);
159             attachment.setFilename(filename);
160             attachment.setPartId(partCount);
161             email.addAttachment(attachment);
162         }
163     }
164 }
165
Popular Tags