1 5 package org.exoplatform.services.communication.message.impl; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import javax.mail.URLName ; 10 import javax.mail.Flags ; 11 import org.exoplatform.services.communication.message.*; 12 17 abstract public class BaseMailMessageProtocolPlugin extends BaseMessageProtocolPlugin { 18 private MailService mailService_ ; 19 20 public BaseMailMessageProtocolPlugin(MessageService mservice, MailService mailService) { 21 super(mservice) ; 22 mailService_ = mailService ; 23 } 24 25 26 public void sendMessage(Account account,Message message) throws Exception { 27 message.setFrom(account.getReplyToAddress()) ; 28 mailService_.sendMessage(message) ; 29 message.setReceivedDate(new java.util.Date ()) ; 30 saveSentMessage(account, message) ; 31 } 32 33 public void synchronize(Account account) throws Exception { 34 String userName = account.getProperty(Account.SERVER_SETTING_USERNAME) ; 35 String password = account.getProperty(Account.SERVER_SETTING_PASSWORD) ; 36 String hostName = account.getProperty(Account.SERVER_SETTING_HOSTNAME) ; 37 URLName url = getInboxFolderURLName(hostName, userName, password); 39 javax.mail.Folder remoteFolder = mailService_.getMailSession().getFolder(url); 40 remoteFolder.open(javax.mail.Folder.READ_WRITE) ; 41 javax.mail.Message [] message = remoteFolder.getMessages() ; 42 List messageToUpdate = new ArrayList () ; 43 Folder inbox = null ; 44 for(int i = 0; i < message.length; i++) { 45 Flags flags = message[i].getFlags(); 46 if(!MailUtil.hasSystemFlag(flags, Flags.Flag.FLAGGED)) { 47 Message newMessage = MailUtil.createMessage(message[i]) ; 48 if(inbox == null) { 49 } 50 inbox = mservice_.getFolder(account, MessageService.INBOX_FOLDER) ; 51 mservice_.createMessage(account, inbox, newMessage) ; 52 messageToUpdate.add(message[i]) ; 53 } 54 } 55 message = new javax.mail.Message [messageToUpdate.size()] ; 56 for(int i = 0; i < message.length; i++) { 57 message[i] = (javax.mail.Message ) messageToUpdate.get(i) ; 58 } 59 Flags flags = new Flags (Flags.Flag.FLAGGED) ; 60 remoteFolder.setFlags(message, flags, true) ; 61 remoteFolder.close(true) ; 62 } 63 64 abstract public URLName getInboxFolderURLName(String host, String userName , String password) ; 65 } | Popular Tags |