1 7 package org.contineo.communication; 8 9 import java.io.FileOutputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.util.Properties ; 13 14 import javax.mail.Address ; 15 import javax.mail.Folder ; 17 import javax.mail.MessagingException ; 18 import javax.mail.Multipart ; 19 import javax.mail.Part ; 20 import javax.mail.Session ; 21 import javax.mail.Store ; 22 import javax.mail.internet.InternetAddress ; 23 24 import org.contineo.communication.dao.EMailDAO; 25 import org.contineo.core.FileBean; 26 import org.contineo.core.config.SettingConfigurator; 27 28 34 public class EMailReceiver { 35 36 42 private EMail email; 43 44 private int partCount; 45 private String userdir; 46 47 50 public EMailReceiver() { 51 email = new EMail(); 52 partCount = 0; 53 userdir = ""; 54 } 55 56 public void receive(EMailAccount account, String username) throws Exception { 57 EMailDAO emailDao = new EMailDAO(); 58 Session sess = Session.getDefaultInstance(new Properties ()); 60 Store store = sess.getStore(account.getProvider()); 61 store.connect(account.getHost(), account.getAccountUser(), account.getAccountPassword()); 62 63 Folder inbox = store.getFolder("INBOX"); 65 if (inbox != null) { 66 inbox.open(Folder.READ_WRITE); 67 68 javax.mail.Message [] messages = inbox.getMessages(); 70 for (int i=0; i<messages.length; i++) { 71 javax.mail.Message message = messages[i]; 72 Part messagePart = message; 73 Object content = messagePart.getContent(); 74 InternetAddress from = ((InternetAddress )message.getFrom()[0]); 75 Address recipients[] = message.getAllRecipients(); 76 77 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 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 } 102 inbox.close(true); 103 } 104 store.close(); 105 } 106 107 private void dumpPart(Part p) throws MessagingException , IOException { 108 if (p.isMimeType("multipart/*")) { 109 Multipart mp = (Multipart )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 cType = p.getContentType(); 118 String 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 mimeType = ""; 128 if (end != -1) 129 mimeType = cType.substring(0, cType.indexOf(";")); 130 else 131 mimeType = cType; 132 InputStream is = p.getInputStream(); 133 FileOutputStream fos = new FileOutputStream (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 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 |